Skip to content

Instantly share code, notes, and snippets.

@pcdv
Created January 29, 2020 10:16
Show Gist options
  • Save pcdv/78921fdcefa7a233fcf22af3ed73bc61 to your computer and use it in GitHub Desktop.
Save pcdv/78921fdcefa7a233fcf22af3ed73bc61 to your computer and use it in GitHub Desktop.
Auto connect to ssh-agent on Windows (Git Bash)
# 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.
#
# Adapted for Git Bash on Windows
GOT_AGENT=0
for FILE in $(find /tmp/ssh-* -type s -user ${USERNAME} -name "agent.[0-9]*" 2>/dev/null)
do
SOCK_PID=${FILE##*.}
PID=$(ps |awk '/ssh-agent/ {print $1}')
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