Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save akesterson/6ff8049df90c1ae5ea223efc64381f82 to your computer and use it in GitHub Desktop.
Save akesterson/6ff8049df90c1ae5ea223efc64381f82 to your computer and use it in GitHub Desktop.
script for automatically managing SSH Agent and keys
#!/bin/bash
function start_sshagent()
{
ssh-agent > ~/.sshagent
. ~/.sshagent
}
function check_sshagent() {
ps -p $SSH_AGENT_PID > /dev/null
if [[ $? -eq 0 ]]; then
echo "SSH Agent running at $SSH_AGENT_PID"
return 0
else
echo "SSH Agent at $SSH_AGENT_PID appears dead"
return 1
fi
}
function add_identities() {
ssh-add ~/.ssh/id_rsa
# If you have extra keys you want to add, add them here
echo "====================================================="
ssh-add -l
}
if [[ ! -f ~/.sshagent ]]; then
start_sshagent
add_identities
check_sshagent
else
. ~/.sshagent
check_sshagent
if [[ $? -eq 1 ]]; then
rm -f ~/.sshagent
start_sshagent
add_identities
check_sshagent
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment