Skip to content

Instantly share code, notes, and snippets.

@beli-sk
Created July 17, 2014 18:19
Show Gist options
  • Save beli-sk/f28d32c3d088b02b7092 to your computer and use it in GitHub Desktop.
Save beli-sk/f28d32c3d088b02b7092 to your computer and use it in GitHub Desktop.
Bash profile snippet for starting and reusing SSH agent on login
### Start SSH agent if not running
SSH_ENV="$HOME/.ssh/environment"
function start_agent {
echo -n "Initialising new SSH agent... "
/usr/bin/ssh-agent -t 3600 | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo done
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}"
/usr/bin/ssh-add
}
# Source SSH settings, if applicable
if [ -f "${SSH_ENV}" ] ; then
. "${SSH_ENV}"
if ! ps -p ${SSH_AGENT_PID} > /dev/null ; then
echo "SSH agent not found, use function start_agent to start a new one."
# cleanup old env
rm "${SSH_ENV}"
unset SSH_AGENT_PID
unset SSH_AUTH_SOCK
else
echo "SSH agent already running."
unset start_agent
fi
else
echo "SSH agent not found, use function start_agent to start a new one."
fi
### end of SSH agent part
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment