Last active
August 29, 2015 14:27
-
-
Save bootleq/0ed444485fe7645b0a69 to your computer and use it in GitHub Desktop.
Find ssh-agent and do ssh-add if needed
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
sshAgentFind() { | |
# Ref https://github.com/wwalker/ssh-find-agent | |
local -a sockets | |
if [[ -z "$SSH_AUTH_SOCK" ]] ; then | |
sockets=("${(@f)$(find /tmp/ -type s -path '/tmp/ssh-*/agent.*' -user $USER)}") | |
for socket in $sockets; do | |
SSH_AUTH_SOCK=$socket ssh-add -l 2> /dev/null > /dev/null | |
if [[ $? -eq 0 ]]; then | |
export SSH_AUTH_SOCK=$socket | |
echo 'ssh agent ready' | |
return 0 | |
fi | |
done | |
if [[ -n $sockets ]]; then | |
export SSH_AUTH_SOCK=$sockets[1] | |
echo 'ssh agent has no key, try `ssh-add` now:'"\n" | |
ssh-add | |
else | |
eval $(ssh-agent -s) > /dev/null | |
echo "New ssh-agent set" | |
fi | |
else | |
echo "No-op, socket already set.\n"'You might want `ssh-add`' | |
fi | |
} | |
[[ -z "$SSH_AUTH_SOCK" ]] && sshAgentFind |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment