diff options
Diffstat (limited to 'bashrc/common/ssh-agent-forwarding.sh')
-rw-r--r-- | bashrc/common/ssh-agent-forwarding.sh | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/bashrc/common/ssh-agent-forwarding.sh b/bashrc/common/ssh-agent-forwarding.sh index e4435ee..14a880f 100644 --- a/bashrc/common/ssh-agent-forwarding.sh +++ b/bashrc/common/ssh-agent-forwarding.sh @@ -1,14 +1,32 @@ -TMPFILE=$(mktemp) - -# cleanup old agent-sockets -if [ -r "${HOME}/.ssh-agent-forwarding" ]; then - cut -d" " -f2 "${HOME}/.ssh-agent-forwarding" | \ - xargs ls -a 2>/dev/null | \ - grep -f - "${HOME}/.ssh-agent-forwarding" | \ - sort -n > "${TMPFILE}" +wipe_removed_sockets() { + TMPFILE=$(mktemp) + + # cleanup old agent-sockets + if [ -r "${HOME}/.ssh-agent-forwarding" ]; then + cut -d" " -f2 "${HOME}/.ssh-agent-forwarding" | \ + xargs ls -a 2>/dev/null | \ + grep -f - "${HOME}/.ssh-agent-forwarding" | \ + sort -n > "${TMPFILE}" - mv "${TMPFILE}" "${HOME}/.ssh-agent-forwarding" -fi + mv "${TMPFILE}" "${HOME}/.ssh-agent-forwarding" + fi +} + +fixssh() { + # if the symlinked socket, does not exists + # find latest connection, that is still alive + wipe_removed_sockets + + if [ ! -e "${HOME}/.ssh-agent-forwarded-sock" ]; then + LATEST_SOCK=$(tail -n 1 "${HOME}/.ssh-agent-forwarding" | cut -d" " -f2) + + if [ -n "$LATEST_SOCK" ]; then + ln -sf "${LATEST_SOCK}" "${HOME}/.ssh-agent-forwarded-sock" + fi + fi +} + +wipe_removed_sockets # if this login is via ssh and ssh-agent is forwarded if [ -n "$SSH_AUTH_SOCK" -a -n "$SSH_CLIENT" ]; then |