Skip to content

Instantly share code, notes, and snippets.

@irgeek
Created January 2, 2015 05:08
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 irgeek/ea676778f6903f63fbaa to your computer and use it in GitHub Desktop.
Save irgeek/ea676778f6903f63fbaa to your computer and use it in GitHub Desktop.
Shell script (bash) to persist ssh-agent environment variables...
# Bash script to maintain a persistent ssh-agent across multiple
# invocations of your shell when the parent process launching them
# does not set the necessary environment variables.
AGENT_VARS_FILE="${HOME}/.ssh/agent-vars.sh"
start_agent() {
rm -f ${AGENT_VARS_FILE}
touch ${AGENT_VARS_FILE}
chmod 600 ${AGENT_VARS_FILE}
[[ -s ${AGENT_VARS_FILE} ]] && { echo "Resetting agent file failed. Non-zero length!"; exit 1; }
ssh-agent | sed 's/^echo/#echo/' >> "${AGENT_VARS_FILE}"
echo "New ssh-agent started - use 'ssh-add' to add keys to it!"
}
agent_running() {
if [[ -n ${SSH_AUTH_SOCK} ]]; then
ssh-add -l > /dev/null 2>&1
case $? in
0) # Everything looks good
return 0
;;
1)
echo "Looks like ssh-agent is running, but it's locked or has no key loaded"
return 0
;;
esac
fi
return 1
}
[[ -r ${AGENT_VARS_FILE} ]] && source ${AGENT_VARS_FILE}
agent_running || start_agent
[[ -r ${AGENT_VARS_FILE} ]] && source ${AGENT_VARS_FILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment