#!/bin/sh # Find or start an ssh-agent, as desired. # # Copyright 2001, Bri Hatch (bri@ifokr.org) # Released under the GPL. # # Version: 1.2. # # Available at # http://www.hackinglinuxexposed.com/tools/find-ssh-agent # # # Purpose: Find and attach to a running SSH agent automatically, # or start one if necessary. # # This code started way back in 1995, and works like a charm # for me. For something with a wider audience, you may want # to look at keychain (www.gentoo.org/proj/en/keychain.xml) # which does a very similar thing, but automatically handles # adding the keys to the agent as well. # # # Theory: # Never assume that ENV variables are sufficient, actually check # to see if an agent is listening. # # Finds agents in the following order: # Agent that are properly set in current ENV. # # Agent specified in ~/.ssh/agent_params # (theoretically the most recently started agent) # # Agents listening in /tmp/ssh-????????/ # (OpenSSH naming convention) # # Agents listening in /tmp/ssh-username/ # (older naming convention) # # Give up and start an agent, if desired, echoing agent # parameters to ~/.ssh/agent_params for next time # # Usage: # # NOTBugs: # This script does not work with csh and friends. If you're using # csh/tcsh/etc, scold yourself and learn Bourne Shell. # # Requirements: # ssh-agent and 'whoami' must be in your PATH already. whoami=`whoami` debug="/bin/false" environ="/bin/false" startagent="/bin/false" usage () { cat <&2 Usage: $0 [options] -e Print environment variables to stdout -s Start agent if one not found -d Print debugging info -h This help text EOM exit 1 } set - `getopt hdes $*` for i in $* do case $i in -d) debug="/bin/true"; shift ;; -e) environ="/bin/true"; shift ;; -s) startagent="/bin/true"; shift ;; -h) usage ;; esac done findAgent () { local SSH_PARAMS if `agentAlive` ; then # Check if we're already set `$debug` && echo "Using existing agent" >&2 else mkdir $HOME/.ssh 2>/dev/null SSH_PARAMS=$HOME/.ssh/agent_params if test -r $SSH_PARAMS; then . $SSH_PARAMS > /dev/null fi if `agentAlive` ; then `$debug` && echo "Connected to existing agent" >&2 else for socket in /tmp/ssh-????????/* /tmp/ssh-$whoami/* do SSH_AUTH_SOCK=$socket; SSH_AUTHENTICATION_SOCKET=$socket; export SSH_AUTH_SOCK export SSH_AUTHENTICATION_SOCKET if `agentAlive` ; then break fi done if `agentAlive` ; then `$debug` && echo "Connected to (unregistered) Agent" >&2 else if `$startagent` ; then eval `ssh-agent -s 2>/dev/null | \ tee $SSH_PARAMS | grep -v '^echo '` if `agentAlive` ; then `$debug` && echo "New Agent started" >&2 else echo "$0: Cannot start the agent" >&2 exit 1 fi else echo "$0: Agent not found." >&2 echo "$0: Use '-s' to force new agent creation" >&2 exit 1 fi fi fi fi } agentAlive () { ssh-add -l > /dev/null 2>&1 # see if it's live [ $? -ne 2 ] } findAgent unset -f findAgent agentAlive `$environ` && echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK ; export SSH_AUTH_SOCK" `$environ` && echo "SSH_AUTHENTICATION_SOCKET=$SSH_AUTH_SOCK ; export SSH_AUTHENTICATION_SOCKET" exit 0;