#!/bin/sh #### ssh-find-agent --- connect to an existing ssh agent, or start one #### Jim Blandy --- May 2000 #### The command #### #### eval `ssh-find-agent` #### #### puts you in touch with an SSH authentication agent. The only #### difference between this and the ordinary ssh-agent command is #### that ssh-find-agent tries to find an existing ssh agent, whereas #### ssh-agent always starts a new one. #### #### If we're already in touch with an SSH agent, don't print anything. progname="$(basename $0)" ### Are we already in touch with an agent? if ssh-add -l > /dev/null 2>&1; then exit 0 fi ### Try to get in touch with the last agent we talked to. contact="/tmp/last-ssh-agent-$USER" if [ -f "$contact" ]; then source "$contact" > /dev/null if ssh-add -l > /dev/null 2>&1; then cat "$contact" exit 0 fi fi ### Start a new agent, and save its contact info. [ ! -f "$contact" -o -w "$contact" ] || { echo "$progname: can't write agent info file: $contact" >&2 exit 1 } ssh-agent > "$contact" || { echo "$progname: couldn't start agent" >&2 exit 1 } source "$contact" > /dev/null || { echo "$progname: error reading new agent contact file: $contact" >&2 exit 1 } ### Verify that we are indeed in contact with the new agent. ssh-add -l > /dev/null || { echo "$progname: started new agent, but couldn't connect to it" >&2 exit 1 } ### Spit out the new contact info. cat "$contact"