Skip to content

Instantly share code, notes, and snippets.

@mcustiel
Created October 19, 2016 11:53
Show Gist options
  • Save mcustiel/d3dd1f9a4f9a8965f98957348d92a9ad to your computer and use it in GitHub Desktop.
Save mcustiel/d3dd1f9a4f9a8965f98957348d92a9ad to your computer and use it in GitHub Desktop.
Start ssh agent if it is not running
# If ssh-agent is not running, start it and add all keys
LINES=$(ps aux | grep ssh-agent | wc -l)
PPKDIR=~/.ssh/keys
PATTERN="\\.pub$"
if [ "2" -gt $LINES ] ; then
ssh-agent -s > ~/.ssh-env-vars
. ~/.ssh-env-vars
for key in $(ls $PPKDIR) ; do
# Add only private keys to ssh-agent
if [[ ! $key =~ $PATTERN ]]; then
ssh-add $PPKDIR/$key
fi
done
else
. ~/.ssh-env-vars
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment