Skip to content

Instantly share code, notes, and snippets.

@fragglet
Last active June 26, 2022 19:30
Show Gist options
  • Save fragglet/21497b2d61c6211b220ff8b95f946cac to your computer and use it in GitHub Desktop.
Save fragglet/21497b2d61c6211b220ff8b95f946cac to your computer and use it in GitHub Desktop.
Track and update SSH_AUTH_SOCK after reconnecting via ssh. Add this to your .bashrc
add_authsock() {
if [[ -L "$SSH_AUTH_SOCK" || ! -e "$SSH_AUTH_SOCK" ]]; then
return
fi
local f c i
mkdir -p "$HOME/.ssh/authsocks"
c=""
for i in $(seq 10); do
f="$HOME/.ssh/authsocks/$i"
if [[ ! -e "$f" ]]; then
c="$f"
elif [[ "$SSH_AUTH_SOCK" = $(readlink "$f") ]]; then
return # already in pool
fi
done
if [[ "$c" != "" ]]; then
ln -sf "$SSH_AUTH_SOCK" "$c"
fi
}
update_authsock_ptr() {
local d
for d in $HOME/.ssh/authsocks/*; do
if [[ -e $d ]]; then
ln -sf "$d" "$HOME/.ssh/authsock"
fi
done
}
add_authsock
update_authsock_ptr
SSH_AUTH_SOCK="$HOME/.ssh/authsock"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment