Created
December 19, 2012 19:12
-
-
Save eiginn/4339567 to your computer and use it in GitHub Desktop.
ssh-agent-import.sh
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
unset SSH_AGENT_PID SSH_ENV_REFRESH | |
[ -r ~/.ssh/env ] && . ~/.ssh/env | |
[ -n "$SSH_AGENT_PID" ] || { | |
killall ssh-agent | |
# No env file (or it's badly corrupted: | |
eval $(ssh-agent 2>/dev/null) &> /dev/null | |
printenv | grep "^SSH_A" | |
SSH_ENV_REFRESH=1 | |
} | |
# Ping the agent process: | |
kill -0 "$SSH_AGENT_PID" >& /dev/null || { | |
# No process, so start a new one: | |
killall ssh-agent | |
eval $(ssh-agent 2>/dev/null) &> /dev/null | |
printenv | grep "^SSH_A" | |
SSH_ENV_REFRESH=1 | |
} | |
ssh-add -l &> /dev/null | |
[ "$?" -gt 1 ] && { | |
# Process alive but unable to be contacted | |
# for some reason (wedged/defunct process, | |
# or damaged/corrupt UNIX domain socket node?) | |
# So kill it: | |
kill "$SSH_AGENT_PID" >& /dev/null | |
# ... with extreme prejudice if necessary: | |
kill -0 "$SSH_AGENT_PID" >& /dev/null \ | |
|| kill -9 "$SSH_AGENT_PID" >& /dev/null | |
# ... and start a new one | |
eval $(ssh-agent &>/dev/null) &> /dev/null | |
SSH_ENV_REFRESH=1 | |
} | |
[ -z "$SSH_ENV_REFRESH" ] || { | |
# Over-write old env file: | |
printenv | grep "^SSH_A" > ~/.ssh/env | |
# Append export command: | |
echo "export SSH_AGENT_PID SSH_AUTH_SOCK" >> ~/.ssh/env | |
# Load the (null-passphrase) identites into the agent: | |
ssh-add < /dev/null &> /dev/null | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment