Skip to content

Instantly share code, notes, and snippets.

@pmakholm
Created July 8, 2009 07:33
Show Gist options
  • Save pmakholm/142660 to your computer and use it in GitHub Desktop.
Save pmakholm/142660 to your computer and use it in GitHub Desktop.
Handle multiple ssh-agents
# Shell snipet to handle multiple ssh-agents
#
# By default I use an ssh-agent with a lot of keys added with the -c option to
# ssh-add and this agent is forwarded trough most of my connections.
# But for some taske this confirmation step is a hassle, so I create new
# agents without confirmation with a minimal set of keys.
#
# You have to source this "script" to work or use the hack described at
# http://peter.makholm.net/2009/07/07/shell-hacks-bash-functions-in-files/
if [ "$1" = "-u" ] ; then
UNBOUND=yes
shift
fi
if [ -d "$HOME/.ssh/$1" ] ; then
export SSH_AGENT_TAG=${1:-std}
SSH_ADD_OPT=
case $SSH_AGENT_TAG in
std)
SSH_ADD_OPT=-c
esac
if [ -n "$UNBOUND" ] ; then
eval $( ssh-agent -s )
ssh-add $SSH_ADD_OPT $HOME/.ssh/$SSH_AGENT_TAG/*
else
if [ ! -e $HOME/.ssh/$SSH_AGENT_TAG.agent ] ; then
eval $( ssh-agent -s -a $HOME/.ssh/$SSH_AGENT_TAG.agent )
ssh-add $SSH_ADD_OPT $HOME/.ssh/$SSH_AGENT_TAG/*
else
SSH_AUTH_SOCK=$HOME/.ssh/$SSH_AGENT_TAG.agent
SSH_AGENT_PID=0
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment