Last active
September 11, 2023 06:42
-
-
Save MarkRose/1772891 to your computer and use it in GitHub Desktop.
Reuse existing ssh-agent or start a new one
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
# Reuse an existing ssh-agent on login, or create a new one. Append this to your .bashrc | |
# I have no idea who the author of the original concept was for reusing agents. This | |
# version also handles the case where the agent exists but has no keys. | |
GOT_AGENT=0 | |
for FILE in $(find /tmp/ssh-* -type s -user ${LOGNAME} -name "agent.[0-9]*" 2>/dev/null) | |
do | |
SOCK_PID=${FILE##*.} | |
PID=$(ps -fu${LOGNAME}|awk '/ssh-agent/ && ( $2=='${SOCK_PID}' || $3=='${SOCK_PID}' || $2=='${SOCK_PID}' +1 ) {print $2}') | |
SOCK_FILE=${FILE} | |
SSH_AUTH_SOCK=${SOCK_FILE}; export SSH_AUTH_SOCK; | |
SSH_AGENT_PID=${PID}; export SSH_AGENT_PID; | |
ssh-add -l > /dev/null | |
if [ $? != 2 ] | |
then | |
GOT_AGENT=1 | |
echo "Agent pid ${PID}" | |
break | |
fi | |
echo "Skipping pid ${PID}" | |
done | |
if [ $GOT_AGENT = 0 ] | |
then | |
eval `ssh-agent` | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please use $TMPDIR instead of "/tmp"
On macos "/tmp" wasnt same as $TMPDIR