Skip to content

Instantly share code, notes, and snippets.

@mkhon
Created June 27, 2022 12:37
Show Gist options
  • Save mkhon/ff695e50b29cd3a369c69d38cd5c7a4c to your computer and use it in GitHub Desktop.
Save mkhon/ff695e50b29cd3a369c69d38cd5c7a4c to your computer and use it in GitHub Desktop.
Find existing ssh-agent or start new
#!/bin/sh
SSH_AUTH_SOCK=
for f in `ls /tmp/ssh-*/agent.*`; do
if SSH_AUTH_SOCK=$f ssh-add -l >/dev/null 2>&1; then
SSH_AUTH_SOCK=$f
echo "Found existing ssh agent SSH_AUTH_SOCK=$SSH_AUTH_SOCK"
break
fi
done
if [ -z "$SSH_AUTH_SOCK" ]; then
echo "Started new ssh agent SSH_AUTH_SOCK=$SSH_AUTH_SOCK"
eval `ssh-agent`
ssh-add
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment