Skip to content

Instantly share code, notes, and snippets.

@romuloceccon
Created July 6, 2017 12:30
Show Gist options
  • Select an option

  • Save romuloceccon/8c0a4e3bad64a4476a0f23cdff3dd019 to your computer and use it in GitHub Desktop.

Select an option

Save romuloceccon/8c0a4e3bad64a4476a0f23cdff3dd019 to your computer and use it in GitHub Desktop.
Small script to start an ssh-agent instance or initialize session with variables from an existing one (put at the end of your .bashrc)
SSH_FILE=$HOME/.ssh-agent-data
if [[ -z "$SSH_AUTH_SOCK" ]]
then
[[ -f "$SSH_FILE" ]] && SSH_VARS=($(cat $SSH_FILE)) || SSH_VARS=()
if [[ -z "${SSH_VARS[@]}" || ! -S "${SSH_VARS[0]}" ]]
then
eval "$(ssh-agent)"
echo $SSH_AUTH_SOCK $SSH_AGENT_PID> $SSH_FILE
else
export SSH_AUTH_SOCK=${SSH_VARS[0]}
export SSH_AGENT_PID=${SSH_VARS[1]}
echo "Agent pid $SSH_AGENT_PID (found)"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment