Skip to content

Instantly share code, notes, and snippets.

@rbriank
Created May 2, 2012 17:48
Show Gist options
  • Save rbriank/2578640 to your computer and use it in GitHub Desktop.
Save rbriank/2578640 to your computer and use it in GitHub Desktop.
Script to run ssh-agent, and ssh-add on login
# Add to ~/.bash_profile
# Start/Reuse SSH Agent - restart or re-use an existing agent
SSH_AGENT_CACHE=/tmp/ssh_agent_eval_`whoami`
if [ -s "${SSH_AGENT_CACHE}" ]
then
echo "Reusing existing ssh-agent"
eval `cat "${SSH_AGENT_CACHE}"`
# Check that agent still exists
kill -0 "${SSH_AGENT_PID}" 2>-
if [ $? -eq 1 ]
then
echo "ssh-agent pid ${SSH_AGENT_PID} no longer running"
# Looks like the SSH-Agent has died, it'll be restarted below
rm -f "${SSH_AGENT_CACHE}"
fi
fi
if [ ! -f "${SSH_AGENT_CACHE}" ]
then
echo "Starting new ssh-agent"
touch "${SSH_AGENT_CACHE}"
chmod 600 "${SSH_AGENT_CACHE}"
ssh-agent >> "${SSH_AGENT_CACHE}"
chmod 400 "${SSH_AGENT_CACHE}"
eval `cat "${SSH_AGENT_CACHE}"`
# add your own key
ssh-add ~/.ssh/some_key_file
fi
@hansenc
Copy link

hansenc commented Nov 23, 2016

For anyone stumbling upon this now, it's no longer necessary. See http://superuser.com/a/269570/393696

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment