From d6930f3a315673107786b9002618dc483eb35014 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Sat, 7 Feb 2009 17:59:05 +0100 Subject: added support for saving and restoring forwarded ssh-agent settings --- bashrc/common/ssh-agent-forwarding.sh | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 bashrc/common/ssh-agent-forwarding.sh (limited to 'bashrc/common') diff --git a/bashrc/common/ssh-agent-forwarding.sh b/bashrc/common/ssh-agent-forwarding.sh new file mode 100644 index 0000000..fc43ee1 --- /dev/null +++ b/bashrc/common/ssh-agent-forwarding.sh @@ -0,0 +1,55 @@ +TMPFILE=$(mktemp) + +# cleanup old agent-sockets +if [ -r "${HOME}/.ssh-agent-forwardin" ]; 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 + +# if this login is via ssh and ssh-agent is forwarded +if [ -n "$SSH_AUTH_SOCK" -a -n "$SSH_CLIENT" ]; then + + # add current socket + echo "$(date +%s) $SSH_AUTH_SOCK $SSH_CLIENT" >> "${TMPFILE}" +fi + +if [ -z "$SSH_AUTH_SOCK" -a -r "${HOME}/.ssh-agent-forwarding" ]; then + SSH_AUTH_SOCK=$(tail -n 1 "${HOME}/.ssh-agent-forwarding" | cut -d" " -f2) + export SSH_AUTH_SOCK +fi + +fixssh() { + if [ ! -r "${HOME}/.ssh-agent-forwarding" ]; then + echo "No ssh-agent forwardings found." + return 1 + fi + + if [ -z "$1" -o "$1" == "latest" ]; then + SSH_AUTH_SOCK=$(tail -n 1 "${HOME}/.ssh-agent-forwarding" | cut -d" " -f2) + else + REMOTE=$(echo $1 | grep -o "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}") + + if [[ "$REMOTE" == "$1" ]]; then + SSH_AUTH_SOCK=$(grep " $REMOTE " "${HOME}/.ssh-agent-forwarding" | head -n 1 | cut -d" " -f2) + else + REMOTE=$(dig +short "$1" | tr '\n' ' ') + + if [ -n "${REMOTE}" ]; then + SSH_AUTH_SOCK=$(grep " ${REMOTE##*: } " "${HOME}/.ssh-agent-forwarding" | head -n 1 | cut -d" " -f2) + else + SSH_AUTH_SOCK=$(grep "$1" "${HOME}/.ssh-agent-forwarding" | head -n 1 | cut -d" " -f2) + fi + fi + fi + + if [ -z "$SSH_AUTH_SOCK" ]; then + echo "Nothing found!" + return 1 + fi + + export SSH_AUTH_SOCK +} -- cgit v1.2.3