Loading...
 

Automatic ssh-agent management

Managing ssh-agent / ssh-add is a pain. If you slap the following into your ~/.bashrc (or equivalent):

Easy ssh-agent / ssh-add system
AGENT_FILE=$HOME/.ssh/ssh_agent
test -e $AGENT_FILE && . $AGENT_FILE
test -n "$SSH_AGENT_PID" && ps --no-headers -f -p $SSH_AGENT_PID | grep -E "^$USER.*ssh-agent$" >/dev/null
if [ $? -gt 0 ]
then
        ssh-agent | grep -v echo > $AGENT_FILE
fi
. $AGENT_FILE
ssh-add -l >/dev/null
if [ $? -gt 0 ]
then
        ssh-add
fi

It'll start the ssh-agent if it's not already running, check to see if you have any keys loaded, and if not will prompt you to load your default identity. Once loaded, you can logout, or login multiple times and you'll always have your identity associated with your session. The persistence is handled through the $AGENT_FILE. Should ssh-agent die or the system reboot, everything will work as you'd expect it to (ie: start new ssh-agent, prompt to load identity again).