tmux and SSH agents

I use tmux a fair amount, together with iterm2's support for control channels, it's amazing. However, when restoring sessions, and you rely on SSH agents, it can sometimes get wonky. The issue being that the shell sessions under the tmux session loses the agent connectivity. So I wrote this little shell script, which I run as part of logging in and starting (or restoring) a tmux session:


#!/bin/sh
 
MY_AGENT=~/.ssh/.ssh-agent
rm -f $MY_AGENT
ln -s $SSH_AUTH_SOCK $MY_AGENT
export SSH_AUTH_SOCK="$MY_AGENT"
 
tmux has-session > /dev/null 2>&1
if [ 0 -eq $? ]; then
    exec tmux -CC attach
else
    exec tmux -CC
fi

 

It might not be perfect, I'm sure it could be automated better in some ways. But with this, naming the script "mux", I simply just run this command every time I want to connect to my tmux session. It'll figure out if it should attach to an existing session, or create a new one as well.

Hacking: