Thursday, October 15, 2009

Setup ssh-agent on multiple machines

I often log into several servers at the same time. It is convenient to use ssh-agent. To setup it automatically, I added following scripts in my .bashrc file:


HOSTNAME=`hostname`
AGENT_ENV_SH=$HOME/.agent_env-$HOSTNAME.sh
AGENT_IS_RUNNING=`ps x | grep ssh-agent | grep -v grep`
if [ -z "$AGENT_IS_RUNNING" ]; then
ssh-agent -s > $AGENT_ENV_SH
fi

if [ -f $AGENT_ENV_SH ]; then
# It is important to redirect output to /dev/null, otherwise, it will confuse scp
source $AGENT_ENV_SH >& /dev/null
fi


After the first login, type 'ssh-add', then never ask you the ssh phrase again.

No comments: