Automatically Adding Keys to Systems without Automatic Key Management

Wed Jul 03 2013 10:31:29 GMT-0400 (EDT)

I've been using this snippet on some of my systems for a while. It's useful for when I want to automatically have ssh-agent started and the user's keys loaded when I login. When this runs in .bash_profile, I don't have to do the ssh-agent /bin/bash && ssh-add dance.

TZ='America/New_York'; export TZ

SSH_ENV="$HOME/.ssh/environment"

function start_agent {
     echo "Initialising new SSH agent..."
     /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
     echo succeeded
     chmod 600 "${SSH_ENV}"
     . "${SSH_ENV}" > /dev/null
     /usr/bin/ssh-add;
}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then
     . "${SSH_ENV}" > /dev/null
     #ps ${SSH_AGENT_PID} doesn't work under cywgin
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
         start_agent;
     }
else
     start_agent;
fi

This snippet was borrowed from Mark Hershberger's blog.