Created
July 17, 2014 18:19
-
-
Save beli-sk/f28d32c3d088b02b7092 to your computer and use it in GitHub Desktop.
Bash profile snippet for starting and reusing SSH agent on login
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### 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