Last active
May 10, 2018 21:44
-
-
Save mchurichi/f7d4f843730c2246fa089617dc89f96b to your computer and use it in GitHub Desktop.
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
SSH_ENV=$HOME/.ssh/environment | |
# start the ssh-agent | |
function start_agent { | |
echo "Initializing new SSH agent..." | |
# spawn ssh-agent | |
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV} | |
chmod 600 ${SSH_ENV} | |
. ${SSH_ENV} > /dev/null | |
/usr/bin/ssh-add | |
} | |
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 | |
# Note: ~/.ssh/environment should not be used, as it | |
# already has a different purpose in SSH. | |
env=~/.ssh/agent.env | |
# Note: Don't bother checking SSH_AGENT_PID. It's not used | |
# by SSH itself, and it might even be incorrect | |
# (for example, when using agent-forwarding over SSH). | |
agent_is_running() { | |
if [ "$SSH_AUTH_SOCK" ]; then | |
# ssh-add returns: | |
# 0 = agent running, has keys | |
# 1 = agent running, no keys | |
# 2 = agent not running | |
ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ] | |
else | |
false | |
fi | |
} | |
agent_has_keys() { | |
ssh-add -l >/dev/null 2>&1 | |
} | |
agent_load_env() { | |
. "$env" >/dev/null | |
} | |
agent_start() { | |
(umask 077; ssh-agent >"$env") | |
. "$env" >/dev/null | |
} | |
if ! agent_is_running; then | |
agent_load_env | |
fi | |
# if your keys are not stored in ~/.ssh/id_rsa.pub or ~/.ssh/id_dsa.pub, you'll need | |
# to paste the proper path after ssh-add | |
if ! agent_is_running; then | |
agent_start | |
ssh-add ~/.ssh/id_rsa_sc | |
elif ! agent_has_keys; then | |
ssh-add ~/.ssh/id_rsa_sc | |
fi | |
unset env |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment