Skip to content

Instantly share code, notes, and snippets.

@ryancurrah
Last active August 21, 2023 19:31
Show Gist options
  • Save ryancurrah/f4fb629905360a2ab6abbf543bbabfb8 to your computer and use it in GitHub Desktop.
Save ryancurrah/f4fb629905360a2ab6abbf543bbabfb8 to your computer and use it in GitHub Desktop.
ssh-agent
#!/bin/zsh
# Check for a currently running instance of ssh-agent
RUNNING_AGENT="$(ps -ax | grep 'ssh-agent' | grep -v grep | wc -l | tr -d '[:space:]')"
if [ "${RUNNING_AGENT}" = "0" ]; then
# Launch a new instance of ssh-agent
eval "$(ssh-agent)"
else
# Connect to the existing ssh-agent instance
export SSH_AUTH_SOCK="$(find /var/folders/*/*/*/ssh-* -name 'agent.*' -user "$(whoami)" 2> /dev/null | head -n 1)"
fi
# Check that ssh keys are loaded
ssh-add -l > /dev/null
if [ "${?}" != "0" ]; then
# No ssh keys are loaded; add the default key
ssh-add -q --apple-load-keychain
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment