Created
May 23, 2023 15:41
-
-
Save akesterson/6ff8049df90c1ae5ea223efc64381f82 to your computer and use it in GitHub Desktop.
script for automatically managing SSH Agent and keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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