Skip to content

Instantly share code, notes, and snippets.

@UnixSage
Created September 7, 2022 13:41
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 UnixSage/5a2073ca910818868f2f2adc05ab3d80 to your computer and use it in GitHub Desktop.
Save UnixSage/5a2073ca910818868f2f2adc05ab3d80 to your computer and use it in GitHub Desktop.
Snipit to add to bash_profile that will find an existing ssh-agent or start a new one. Also complains if it finds more than one for a user.
# Attach to a running ssh-agent or start a new one
if [ `find /tmp -maxdepth 1 -name "ssh-*" -user ${USER} | wc -l` == 1 ] ; then
echo "Found ssh-agent, attaching.."
SSHDIR=`find /tmp -maxdepth 1 -name "ssh-*" -user ${USER}`
SSH_AUTH_SOCK=`ls ${SSHDIR}/agent*` ; export SSH_AUTH_SOCK
SSH_AGENT_PID=`echo ${SSH_AUTH_SOCK} | awk -F"." '{print $2+1}'` ; export SSH_AGENT_PID
echo "Agent pid ${SSH_AGENT_PID}"
elif [ `find /tmp -maxdepth 1 -name "ssh-*" -user ${USER} | wc -l` == 0 ] ; then
echo "no ssh-agent found, starting.."
eval `ssh-agent -t 86400`
else
echo "####################################"
echo "# ssh-sgent in a suspicious state! #"
echo "####################################"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment