Skip to content

Instantly share code, notes, and snippets.

@statico
Created August 8, 2011 21:47
Show Gist options
  • Save statico/1132839 to your computer and use it in GitHub Desktop.
Save statico/1132839 to your computer and use it in GitHub Desktop.
Starting an ssh-agent once and restoring the session later. A really lightweight version of the `keychain` script.
# A function which restarts the ssh-agent, saves the output to a file which can
# be sourced later, and adds the default ssh key.
sshenv=$HOME/.sshlocal
function init-ssh-agent() {
# Kill the agent if it's running.
if [ -n "$SSH_AGENT_PID" ]; then
echo "Killing existing agent $SSH_AGENT_PID..."
kill $SSH_AGENT_PID
fi
# Run the agent and add the key.
ssh-agent -s > $sshenv
. $sshenv
ssh-add
}
if [ "$(hostname)" == "xxxx" ]; then
if [ -e $sshenv ]; then
echo "Restoring ssh-agent environment..."
. $sshenv
fi
# If there is no ssh-agent PID or it's set but not running, restart it.
if [ -z "$SSH_AGENT_PID" -o ! -e /proc/$SSH_AGENT_PID ]; then
echo "SSH agent doesn't seem to be running. Restarting..."
init-ssh-agent
fi
fi
@statico
Copy link
Author

statico commented Aug 8, 2011

I'm an idiot. Just use Keychain. http://docs.funtoo.org/wiki/Keychain

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment