Skip to content

Instantly share code, notes, and snippets.

@stormbeta
Created November 16, 2016 22:38
Show Gist options
  • Save stormbeta/ebc2b5cba878df8c775924113e42b69e to your computer and use it in GitHub Desktop.
Save stormbeta/ebc2b5cba878df8c775924113e42b69e to your computer and use it in GitHub Desktop.
ssh-agent-settings
function start_agent {
# Initialize new agent and add authentication
echo "Initialising new SSH agent..."
# Start authenticating daemon
# No authentications set up yet, just starting daemon!
ssh-agent | head -2 > ${SSH_ENV}
chmod 600 ${SSH_ENV}
# Find SSH_AUTH_SOCK and SSH_AGENT_PID of the available daemon
source ${SSH_ENV} > /dev/null
# Add authentication to this and only this daemon
ssh-add
}
function ssh-login {
if [[ -f "${SSH_ENV}" ]]; then
# Find SSH_AUTH_SOCK and SSH_AGENT_PID of the available daemon
source ${SSH_ENV} > /dev/null
# Check if the agent is still running
local ierr=0
ps ${SSH_AGENT_PID} > /dev/null || ierr=1
if [ ${ierr} == "0" ]; then
echo > /dev/null
else
# If not initialize new agent and
# add authentication
start_agent;
fi
else
start_agent;
fi
}
case ${PLATFORM} in
darwin)
# Setup ssh stuff.
if [[ -z ${SSH_AUTH_SOCK} ]]; then
ssh-add -K
else
echo "SSH agent using key in OSX keychain."
fi
;;
*)
# Setup ssh agent.
if [[ -z ${SSH_AUTH_SOCK} ]] && [[ -e "${SSH_AUTH_SOCK}" ]] ; then
export SSH_ENV=${HOME}/.ssh/env-${HOSTNAME}
export SSH_CONFIG=${HOME}/.ssh/config
ssh-login
else
echo "SSH agent already active from another session or host."
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment