fixing screen and ssh-agent

statico on 2005-10-12T03:20:20

The Problem

I use Screen to leave programs running and an SSH key agent to shell between machines and use CVS without reentering my password. Disconnnecting from the server that the screen session is running on removes the connection to the SSH agent.

What should happen:

  • I should be able to run a command, say latestssh, to reattach my current SSH agent to the screen window
  • New screen windows should pick up on the new agent immediately

The Solution

A shell snippet (Zsh, but easily portable):

local agentdir=~/.latestssh
local agentfile=$agentdir/$HOST.sh
mkdir -p $agentdir
chmod 0700 $agentdir >/dev/null

if [ -n "$SSH_AUTH_SOCK" -a -z $STY ]; then
    echo "export SSH_AUTH_SOCK=$SSH_AUTH_SOCK" >$agentfile
    chmod 0600 $agentfile >/dev/null
fi

# ...existing windows can run this alias
alias latestssh="source $agentfile; ls \$SSH_AUTH_SOCK"

# ...new windows get it automatically
if [ -n "$STY" ]; then
    source $agentfile
fi


ooh that annoys me...

WebDragon on 2005-10-14T23:13:27

yes I'd very much like to know where in the whole process your script gets worked into the flow -- you didn't mention and I'm (apparently) not savvy enough to grok where I should do it. :)

Have I mentioned that you rock? heh

Re:ooh that annoys me...

statico on 2005-10-15T04:23:25

That snippet is simply plopped into my .zshrc file. New screen windows magically receive the new agent, and I can run the "latestssh" command in existing windows.

If you're wondering how to set up an SSH key agent, there are lots of tutorials out there. I use the widely-distributed "keychain" utility as well.