Skip to content

Instantly share code, notes, and snippets.

@mfdela
Created May 27, 2013 12:03
Show Gist options
  • Save mfdela/5656719 to your computer and use it in GitHub Desktop.
Save mfdela/5656719 to your computer and use it in GitHub Desktop.
Launching ssh-agent on a remote host and reusing the same agent for all ssh connections.
ssh-launch-agent() {
echo "Starting ssh-agent for remote connection..."
eval `ssh-agent -s -a "$HOME/.ssh/ssh-auth-remote.sock"`
echo $SSH_AGENT_PID > "$HOME/.ssh/ssh-agent-remote.pid"
ssh-add
}
if [ ! -z "$SSH_TTY" ]; then
[ ! -f "$HOME/.ssh/ssh-agent-remote.pid" ] && ssh-launch-agent
pid=`cat "$HOME/.ssh/ssh-agent-remote.pid"`
if [ ! -d /proc/$pid ]; then
# restarting ssh-agent
ssh-launch-agent
else
# existing agent running
export SSH_AGENT_PID=$pid
export SSH_AUTH_SOCK="$HOME/.ssh/ssh-auth-remote.sock"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment