Skip to content

Instantly share code, notes, and snippets.

@benders
Created January 16, 2013 20:16
Show Gist options
  • Save benders/4550480 to your computer and use it in GitHub Desktop.
Save benders/4550480 to your computer and use it in GitHub Desktop.
bashrc voodoo for consistently connecting to ssh-agent
# ssh-agent needs to be running and we need to export the SSH_AUTH_SOCK
# environment variable to point to its location in /tmp/ssh-something/agent.PID
# so, find the ssh-agent PID and derive the socket filename from it, and then
# find that file in its randomly-named directory in /tmp and export that for
# ssh to use.
set -x
SSH_AGENT_PID=`pgrep -n -u $USER ssh-agent`
if [ -n "$SSH_AGENT_PID" ]; then
guessed_ppid=`expr $SSH_AGENT_PID - 1`
export SSH_AGENT_PID
export SSH_AUTH_SOCK=`find /tmp -user $USER -name agent.$guessed_ppid -type s 2> /dev/null`
else
eval `ssh-agent`
fi
ssh-add
set +x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment