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 # 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" >> "${HOME}/.ssh-agent-forwarding" # remove old symlink if [[ -L "${HOME}/.ssh-agent-forwarded-sock" ]]; then rm -f "${HOME}/.ssh-agent-forwarded-sock" fi # create static symlink to socket if [[ ! -e "${HOME}/.ssh-agent-forwarded-sock" ]]; then ln -sf "$SSH_AUTH_SOCK" "${HOME}/.ssh-agent-forwarded-sock" else echo "Warning: ${HOME}/.ssh-agent-forwarded-sock exists and is not a symlink!" fi fi SSH_AUTH_SOCK="${HOME}/.ssh-agent-forwarded-sock" export SSH_AUTH_SOCK