-
-
Save pythoninthegrass/fb5ffe9cb74060aafeb3462d83244467 to your computer and use it in GitHub Desktop.
Lazy Loading for ssh-agent keys. Loads default SSH identities on demand. Tested with bash and zsh.
This file contains hidden or 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
function wrapssh() { | |
setopt shwordsplit &>/dev/null | |
# load agent if it's not running | |
if [ -z "$SSH_AUTH_SOCK" ]; then | |
eval $(ssh-agent) &>/dev/null | |
fi | |
# load keys if necessary | |
DO_ADD=0 | |
LOADED=$(ssh-add -L | awk '{print($3)}') | |
for x in $HOME/.ssh/id_rsa $HOME/.ssh/id_dsa $HOME/.ssh/id_ecdsa; do | |
if [ -r $x ]; then | |
FOUND=0 | |
for l in $LOADED; do | |
if [ "${x}" = "${l}" ]; then | |
FOUND=1 | |
fi | |
done | |
if [ $FOUND -eq 0 ]; then | |
DO_ADD=1 | |
fi | |
fi | |
done | |
if [ $DO_ADD -eq 1 ]; then | |
ssh-add 2>/dev/null | |
fi | |
"$@" | |
} | |
alias ssh='wrapssh ssh' | |
alias scp='wrapssh scp' | |
alias rsync='wrapssh rsync' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment