Skip to content

Instantly share code, notes, and snippets.

@aflondono
Forked from MarkRose/reuse_agent.sh
Last active September 27, 2020 23:38
Show Gist options
  • Save aflondono/524479208ea5733041363bd7ffe7c5d8 to your computer and use it in GitHub Desktop.
Save aflondono/524479208ea5733041363bd7ffe7c5d8 to your computer and use it in GitHub Desktop.
Reuse existing ssh-agent or start a new one
#!/usr/bin/env bash
# This is a fork created to be used with Git Bash that comes with the installation
# of Git for Windows (https://gitforwindows.org/), and it was tested only on that
# environment.
# Append this to your .bashrc to reuse an existing ssh-agent, or if you don't have
# a .bashrc file (normally located in Windows under 'C:\Users\username'), then
# make this your .bashrc file.
LOGNAME=username
GOT_AGENT=0
# List all the files that follow the SSH agent socket pattern, with the newest
# one at the top of the list. The top one is probably the one we want to use
for FILE in $(ls -t /tmp/ssh-*/agent.[0-9]* 2>/dev/null)
do
# As the values in FILE are socket file names, the `ls` command adds a `=`
# at the end of the string. Here we are removing that last character.
SOCK_FILE=${FILE%?}
# Now check if there are intances of the SSH agent running
PIDLIST=($(ps -fu $LOGNAME | awk '/ssh-agent/ {print $2;}'))
COUNT=${#PIDLIST[@]}
if [ $COUNT -gt "1" ]
then
echo "Multiple ($COUNT) ssh-agents are running. The first available will be selected"
fi
for PID in "${PIDLIST[@]}"
do
# If PID was not set, get out of here to start a new ssh-agent
if [ -z $PID ]
then
echo "No ssh-agent is running. Will start new agent..."
break
else
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
fi
done
if [ $GOT_AGENT = 1 ]
then
break
fi
echo "Skipping file $FILE"
done
# If no ssh-agent was found, then start one and add a key. If your key is not in
# the default location, or if it doesn't use the default name ("~/.ssh/id_rsa" or
# in Windows "C:\Users\username\.ssh\id_rsa"), then add the correct path and key
# name to the `ssh-add` command. Like `ssh-add /c/your/key/path/my_id_rsa_key`
if [ $GOT_AGENT = 0 ]
then
eval `ssh-agent`
ssh-add
fi
#!/usr/bin/env bash
# Set the already running ssh-agent so that the Windows environment recognises it,
# i.e. Visual Studio 2017 recognises the ssh-agent.
# This will need to be run after an ssh-agent was already started and set, and before running
# Visual Studio 2017.
# I have set up my Git-Bash so that just starting it will start a new ssh-agent, or set one if
# one is already running.
# So first run Git-Bash, which will set the variables used below.
# Then run this script, and close Git-Bash.
# Finally start VS 2017.
# Source of this code:
# https://stackoverflow.com/questions/48885315/get-visual-studio-2017-to-work-with-ssh-agent-ssh-add-for-git/49071597
if [[ `uname` == MINGW* ]]
then
setx SSH_AUTH_SOCK $SSH_AUTH_SOCK > /dev/null
setx SSH_AGENT_PID $SSH_AGENT_PID > /dev/null
echo "Done. Close Git-Bash for the change to take effect, and restart Visual Studio."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment