DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Ssh-agent Script
This checks to see if you have ssh-agent running already, and if it's not, it starts it for you.
This may be taking a risk by writing your environment settings to a file that someone can read.
I don't tend to use this one anymore, I have a more sneaky one now:
http://snippets.dzone.com/posts/show/10897
I figured out that you don't need to record the environment,
you can actually re-create it based on what files are in /tmp
#!/bin/bash
SSH_ENV="$HOME/.ssh/environment.$HOSTNAME"
if [ -x /usr/bin/ssh-agent ]
then
SSH_AGENT=/usr/bin/ssh-agent
SSH_ADD=/usr/bin/ssh-add
else
echo "Can't find ssh-agent"
SSH_AGENT=/bin/false
SSH_ADD=/bin/false
fi
start_agent () {
printf "Starting new SSH agent... "
$SSH_AGENT > "${SSH_ENV}"
if [ $? = 0 ]
then
echo "OK"
printf "3s|^echo|#echo|\nw\n\q\n" | ed "${SSH_ENV}" >/dev/null 2>&1
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}"
else
echo "ERROR"
fi
$SSH_ADD -l | grep : || {
$SSH_ADD;
}
}
#
# Source SSH settings, if there
#
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}"
#ps ${SSH_AGENT_PID} doesn't work under cywgin
ps -fu$LOGNAME | grep ${SSH_AGENT_PID}.*ssh-agent$ >/dev/null
if [ $? != 0 ]
then
start_agent;
else
$SSH_ADD -l | grep : || {
echo "Agent is running, but has no keys..."
$SSH_ADD
}
fi
else
start_agent;
fi
And add this to .bashrc / .kshrc
alias ssh_agent='. $HOME/.ssh/ssh_agent' SSH_ENV="$HOME/.ssh/environment.$HOSTNAME





