Skip to content

Instantly share code, notes, and snippets.

@ndowens
Forked from j1cs/ssh-agent-keys.sh
Created January 22, 2020 23:34
Show Gist options
  • Save ndowens/9adedcb563391acaf69ea212fab58e6a to your computer and use it in GitHub Desktop.
Save ndowens/9adedcb563391acaf69ea212fab58e6a to your computer and use it in GitHub Desktop.
SSH_ENV=$HOME/.ssh/environment
function start_agent {
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}";
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
adding_keys
}
function adding_keys {
for possiblekey in ${HOME}/.ssh/*; do
if grep -q PRIVATE "$possiblekey"; then
ssh-add -l | grep -q `ssh-keygen -lf "$possiblekey" | awk '{print $2}'` || ssh-add "$possiblekey" > /dev/null 2>&1
fi
done
}
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
rm "${SSH_ENV}";
start_agent;
}
else
start_agent;
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment