Skip to content

Instantly share code, notes, and snippets.

@drbobbeaty
Created June 9, 2011 14:03
Show Gist options
  • Save drbobbeaty/1016785 to your computer and use it in GitHub Desktop.
Save drbobbeaty/1016785 to your computer and use it in GitHub Desktop.
Little Script to Start SSHAgent for SSH Logins
#!/bin/tcsh
##
# Start SSH Key Agent
##
if (`where ssh-agent` != "") then
#
# See if there's already a running copy of ssh-agent
#
set proc=`ps -aux | grep 'ssh-agent' | grep -v grep`
if ($%proc >= 10) then
set pid=`echo "${proc}" | awk '{ print $2 }'`
echo 'Killing existing ssh-agent at '${pid}
kill ${pid}
endif
#
# ...and make sure to unset the variable for the PID of the agent
#
if ($?SSH_AGENT_PID) then
unsetenv SSH_AGENT_PID
endif
#
# Now see if we have the socket connection already defined as well
#
if ($?SSH_AUTH_SOCK) then
if (! -S "${SSH_AUTH_SOCK}") then
unsetenv SSH_AUTH_SOCK
endif
endif
#
# This is the file location that will hold the environment-setting
# commands for all subsequent shells based on the results of running
# ssh-agent for the first time.
#
setenv SSH_AGENT_STATE "/tmp/.ssh-agent-state.${user}"
#
# If it's still there, it's got old data and needs to be wiped out
#
if (-f "${SSH_AGENT_STATE}") then
rm -f "${SSH_AGENT_STATE}"
endif
#
# If we're all clean, then we need to start up a new instance, and
# save the environment settings in the proper file for later
# invocation by other shells.
#
if (! $?SSH_AGENT_PID && ! $?SSH_AUTH_SOCK && ! -f "${SSH_AGENT_STATE}") then
echo 'Starting ssh-agent...'
ssh-agent | grep -v '^echo ' > "${SSH_AGENT_STATE}"
source "${SSH_AGENT_STATE}"
endif
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment