Skip to content

Instantly share code, notes, and snippets.

@routevegetable
Last active July 12, 2022 17:52
Show Gist options
  • Save routevegetable/1b881bf53506316762e5b1a80848b1f1 to your computer and use it in GitHub Desktop.
Save routevegetable/1b881bf53506316762e5b1a80848b1f1 to your computer and use it in GitHub Desktop.
ssh things
SOCK_LINK=/tmp/ssh_auth_sock_link
start_ssh_agent() {
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
ln -vsnf $SSH_AUTH_SOCK $SOCK_LINK
}
if [[ $- == *i* ]] && ! [[ -e $SOCK_LINK ]]; then
# Agent socket link doesn't exist - assume agent isn't running
start_ssh_agent
fi
export SSH_AUTH_SOCK=$SOCK_LINK
SOCK_LINK=/tmp/ssh_auth_sock_link
SOCK_LOCK=/tmp/sock_lock
if [[ $- == *i* ]] && [[ $SSH_AUTH_SOCK ]] && [[ $SSH_AUTH_SOCK != $SOCK_LINK ]]; then
# We have a sock
# Open a lock for our sock
exec {FD}<>$SOCK_LOCK
# Flock our sock lock
if ! flock -x -w 2 $FD; then
echo "Failed to flock the sock lock"
else
# Sock lock has been flocked!
ln -vsnf $SSH_AUTH_SOCK $SOCK_LINK
flock -u $FD
fi
fi
export SSH_AUTH_SOCK=$SOCK_LINK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment