Skip to content

Instantly share code, notes, and snippets.

@samv
Created September 12, 2011 21:42
Show Gist options
  • Save samv/1212543 to your computer and use it in GitHub Desktop.
Save samv/1212543 to your computer and use it in GitHub Desktop.
SSH AUTH SOCK setter
# ~/bin/start-ssh-agent.sh
# make sure your .bashrc sources this file, I use:
#
# if [ -t ]
# then
# . ~/bin/start-ssh-agent.sh
# fi
#
# Old versions of SSH used to call the variable
# SSH_AUTHORIZATION_SOCKET; this is no longer supported.
if [ "$1" = "-q" ]
then
RC=1
shift
fi
PATTERN=$1
[ -z "$PATTERN" ] && PATTERN=:
OLD_SSH_AUTH_SOCK="$SSH_AUTH_SOCK"
SSH_AUTH_SOCK=
if [ -d "`ls -d /tmp/ssh-* 2>/dev/null | head -1`" ]
then
for x in "$OLD_SSH_AUTH_SOCK" /tmp/ssh-*/*
do
#echo -n Checking $x ...
[ -S $x ] || continue #( echo not a socket, ignoring; continue )
export SSH_AUTH_SOCK=$x
if [ -n "`ssh-add -l 2>/dev/null | grep \"$PATTERN\"`" ]
then
#echo that\'ll do!
found="$x"
break
#else
#echo no relevant identities, next!
fi
done
if [ -z "$found" ]
then
for x in "$OLD_SSH_AUTH_SOCK" /tmp/ssh-*/*
do
#echo -n Checking $x ...
[ -S $x ] || continue #( echo not a socket, ignoring; continue )
export SSH_AUTH_SOCK=$x
if [ -n "$(ssh-add -l 2>&1 | grep -i 'no identities')" ]
then
found="$x"
break
fi
done
fi
fi
if [ -z "$SSH_AUTH_SOCK" ]
then
#echo Starting a new agent, as \$SSH_AUTH_SOCK is \""$SSH_AUTH_SOCK"\"
eval $(ssh-agent -s 2>/dev/null)
fi
export SSH_AUTH_SOCK SSH_AGENT_PID
if [ -z "$RC" ]
then
echo "SSH_AUTH_SOCK is $SSH_AUTH_SOCK"
ssh-add -l
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment