Skip to content

Instantly share code, notes, and snippets.

@jbreams
Created August 12, 2015 19:22
Show Gist options
  • Save jbreams/6f50a11882b9cdddb812 to your computer and use it in GitHub Desktop.
Save jbreams/6f50a11882b9cdddb812 to your computer and use it in GitHub Desktop.
# Add this to your .bashrc to automatically detect running ssh-agents and connect to them.
if [ "x$SSH_AGENT_PID" = "x" ]; then
# Set the ssh-agent PID to the oldest ssh-agent owned by the current user
export SSH_AGENT_PID=$(pgrep -U $USER -o ssh-agent)
if [ "x$SSH_AGENT_PID" = "x" ]; then
eval $(ssh-agent)
else
# Find the auth socket for this PID in /tmp
export SSH_AUTH_SOCK=$(find /tmp/ssh-* -type s -name agent.$((SSH_AGENT_PID - 1)) -print -quit 2>/dev/null)
fi
if [ "x$SSH_AUTH_SOCK" = "x" ]; then
echo "Error setting up ssh-agent. You're gonna have to type a lotta passwords!"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment