Skip to content

Instantly share code, notes, and snippets.

@jhsu
Created March 18, 2011 16:57
Show Gist options
  • Save jhsu/876417 to your computer and use it in GitHub Desktop.
Save jhsu/876417 to your computer and use it in GitHub Desktop.
#!/bin/bash
# from http://snippets.dzone.com/posts/show/10897
# source this file from bashrc
GOT_AGENT=0
for FILE in $(find /tmp/ssh-???*[0-9]* -type s -user ${LOGNAME} -name "agent.[0-9]*")
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 | grep : > /dev/null
if [ $? = 0 ]
then
GOT_AGENT=1
echo "Agent pid ${PID}"
break
fi
echo "Skipping pid ${PID}"
done
if [ $GOT_AGENT = 0 ]
then
ssh-add
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment