Skip to content

Instantly share code, notes, and snippets.

@carldanley
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carldanley/4905f8299dde0f996554 to your computer and use it in GitHub Desktop.
Save carldanley/4905f8299dde0f996554 to your computer and use it in GitHub Desktop.
SSH Agent Scripts
# adds all keys in the ~/.ssh folder
function add_ssh_keys {
for i in `find ~/.ssh -name '*.pub' | sed 's/.\{4\}$//'`;
do ssh-add $i;
done
}
SSH_ENV=$HOME/.ssh/environment
function start_agent {
echo "Initializing new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
add_ssh_keys;
}
function stop_agent {
if [ ${SSH_AGENT_PID+1} == 1 ]; then
ssh-add -D
ssh-agent -k > /dev/null 2>&1
unset SSH_AGENT_PID
unset SSH_AUTH_SOCK
fi
}
function restart_agent {
stop_agent;
start_agent;
}
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment