#!/bin/bash # # Script to detect ssh-agent(s) # Usage: # . ./this.sh # # It is perhaps worth reiterating that this script should be sourced # rather than run so that the environment variables it sets are made # available. (eg: . ./this.sh or source ./this.sh rather than just ./this.sh) # # Robert McKay Wed Nov 15 12:30:18 GMT 2006 # # Specify the fingerprint of the key you want to find an agent for # If blank, it will use the first agent detected. #ident="xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" ident="" : ${USER:=$(whoami)} sockets=$( find /tmp/ssh-* -user ${USER} -name 'agent.*' -type s 2>/dev/null ) for sock in ${sockets}; do agent_ppid=$( basename ${sock} | cut -f2 -d"." ) agent_pid=$((agent_ppid+1)) ps hocmd,pid p ${agent_pid} | grep -q ssh-agent || continue; if SSH_AGENT_PID=${agent_pid} SSH_AUTH_SOCK=${sock} ssh-add -l |\ grep -qi "${ident}"; then SSH_AGENT_PID=${agent_pid} SSH_AUTH_SOCK=${sock} export SSH_AGENT_PID SSH_AUTH_SOCK break; fi done