Skip to content

Instantly share code, notes, and snippets.

@jolbax
Last active January 13, 2017 10:13
Show Gist options
  • Save jolbax/c1af7d0409b3086730dde129864042a5 to your computer and use it in GitHub Desktop.
Save jolbax/c1af7d0409b3086730dde129864042a5 to your computer and use it in GitHub Desktop.
Using an ssh-agent in a script
#!/bin/bash
tempfile=/tmp/ssh-agent.test
#Check for an existing ssh-agent
if [ -e $tempfile ]
then
echo "Examining old ssh-agent"
. $tempfile
fi
#See if the agent is still working
ssh-add -l > /dev/null
#If it's not working yet, just start a new one.
if [ $? != 0 ]
then
echo "Old ssh-agent is dead..creating new agent."
#Create a new ssh-agent if needed
ssh-agent -s > $tempfile
. $tempfile
#Add the key
ssh-add
fi
#Show the user which keys are being used.
ssh-add -l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment