Created
December 13, 2013 05:36
-
-
Save psyllo/7940194 to your computer and use it in GitHub Desktop.
ssh-agent-start.sh - handy for starting an ssh-agent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Author: Benjamin Cluff - 2013 | |
# | |
# Synopsis: | |
# | |
# Start ssh-agent and create file containing the environment | |
# variables that can be sourced by .bash_profile so that other ssh | |
# commands (ssh, ssh-add) can find it. | |
# | |
# A new bash shell is started with the ssh-agent env vars, but it is | |
# not necessary if the file with the env vars is sourced. | |
# | |
# Messages are piped to stderr so stout is safe to evaluate. | |
# | |
ssh_agent_env_file=~/.ssh-agent-env | |
if [ -r $ssh_agent_env_file ]; then | |
source $ssh_agent_env_file >&2 | |
fi | |
if [ -n "$SSH_AGENT_PID" ]; then | |
echo "Another ssh-agent may be running with PID: $SSH_AGENT_PID" >&2 | |
echo "Attempting to 'ssh-agent kill $SSH_AGENT_PID' first." >&2 | |
ssh-agent kill $SSH_AGENT_PID >&2 | |
sleep 2 | |
fi | |
ssh-agent > $ssh_agent_env_file | |
if [ $? == 0 ]; then | |
echo "Example for adding your key to agent: ssh-add ~/.ssh/id_rsa" >&2 | |
echo 'Starting new shell with updated env vars...' >&2 | |
source $ssh_agent_env_file > /dev/null 2>&1 | |
bash >&2 | |
echo | |
echo "Leaving bash shell created by '$0'" >&2 | |
echo 'You can evaulate the following to set the env vars in this shell:' >&2 | |
echo | |
cat $ssh_agent_env_file | |
echo | |
else | |
echo 'There was a problem starting the ssh-agent.' >&2 | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In 2008, I have come up with this (funnily same named) solution; it also does automatically add your keys.
Its to be dropped in your personal
~/bin
folder and then called in~/.bashrc
like this:~/bin/ssh_agent_start.sh
contains: