Skip to content

Instantly share code, notes, and snippets.

@neraliu
Forked from alexras/ssh-agent-snippets.sh
Created September 30, 2015 03:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neraliu/8e46da4556d52deda64e to your computer and use it in GitHub Desktop.
Save neraliu/8e46da4556d52deda64e to your computer and use it in GitHub Desktop.
Bash snippets to automatically start and stop an ssh-agent process on login and logout
#!/bin/bash
## in .bash_profile
SSHAGENT=`which ssh-agent`
SSHAGENTARGS="-s"
if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then
eval `$SSHAGENT $SSHAGENTARGS`
trap "kill $SSH_AGENT_PID" 0
fi
## in .logout
if [ ${SSH_AGENT_PID+1} == 1 ]; then
ssh-add -D
ssh-agent -k > /dev/null 2>&1
unset SSH_AGENT_PID
unset SSH_AUTH_SOCK
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment