#!/usr/bin/perl ########################################################################### # # sshagent.pl Edward Diehl Sept, 2006 V. 1.1 # # This script is a frontend for ssh-agent to make it easier to use. # The script does the following: # 1. Check env. variable SSH_AGENT_PID is already set. # If so assume that all is ok and so quit. # 2. If there is no SSH_AGENT_PID then look for ssh-agent processes. # If found config for it, i.e. set SSH_AGENT_PID and SSH_AUTH_SOCK, # and list any loaded keys. # 3. If no ssh-agent is found then start one, and load ~/.ssh/id_rsa # if it exists # # To set these variables in the parent shell you must invoke # this script as: eval `sshagent.pl`. If you just run the script straight # you see the C-shell commands it generates, but variables are not set. # # To kill an existing ssh-agent run with the the -k option. # ########################################################################### # Check for arguments ########################################################################### if( $ARGV[0] eq "-h" || $ARGV[0] eq "--help" ) { print "echo '======== Script sshagent.pl Author: Edward Diehl =======';\n"; print "echo ' This script is a frontend for ssh-agent to make it easier to use.';\n"; print "echo ' The script does the following:';\n"; print "echo ' 1. Check env. variable SSH_AGENT_PID is already set.';\n"; print "echo ' If so assume that all is ok and so quit.';\n"; print "echo ' 2. If there is no SSH_AGENT_PID then look for ssh-agent processes.';\n"; print "echo ' If found config for it, i.e. set SSH_AGENT_PID and SSH_AUTH_SOCK';\n"; print "echo ' 3. If no ssh-agent is found then start ssh-agent.';\n"; print "echo '';\n"; print "echo ' To set these variables in the parent shell you must invoke';\n"; print "echo ' this script as: eval `sshagent.pl`. If you just run the script straight';\n"; print "echo ' you see the C-shell commands it generates, but variables are not set.';\n"; print "echo '';\n"; print "echo ' To kill an existing ssh-agent run with the the -k option.'\n"; exit; } elsif ( $ARGV[0] eq "-k" ) { system("ssh-agent -k"); exit; } ########################################################################### # See if SSH_AGENT_PID is already set in which case there is nothing to do ########################################################################### $ssh_pid = $ENV{SSH_AGENT_PID}; if( $ENV{SSH_AGENT_PID} != "" ) { $ssh_sock = $ENV{SSH_AUTH_SOCK}; print "echo ssh-agent already running and configured:;\n"; print "echo ' SSH_AGENT_PID = $ssh_pid';\n"; print "echo ' SSH_AUTH_SOCK = $ssh_sock';\n"; print "echo SSH Keys loaded:;\n"; print "echo `ssh-add -l`\n"; exit; } ########################################################################### # Get a list of all ssh-agents running ########################################################################### @list = `ps -C ssh-agent -o pid=,user=`; $user = $ENV{USER}; $found_pid = 0; ########################################################################### # Check if one of these agents is owned by the user ########################################################################### foreach $line (@list) { if( $line =~ /(\d+)\s+$user/ ) { $ssh_pid = $1; $found_pid++; print "echo Found ssh-agent pid=$ssh_pid for user $user;\n" } } ########################################################################### # If ssh-agent exists, find the /tmp/ssh* file which has the socket # NOTE: use 'ls' rather than 'find' because find generates stupid errors # when looking at non-user-owned files in /tmp ########################################################################### if( $found_pid == 1 ) { @list = `ls -ald /tmp/ssh-*`; $found_sock = 0; foreach $line (@list) { if( $line =~ /$user/ ) { if( $line =~ /ssh-(\w+)/ ) { $ssh_sock = `find /tmp/ssh-$1/ -name "agent.*"`; chop $ssh_sock; $found_sock++; } } } ########################################################################### # If found ssh-agent exists, print details ########################################################################### if( $found_sock == 1 ) { print "setenv SSH_AGENT_PID $ssh_pid;\n"; print "setenv SSH_AUTH_SOCK $ssh_sock;\n"; print "echo SSH_AGENT_PID = $ssh_pid;\n"; print "echo SSH_AUTH_SOCK = $ssh_sock;\n"; print "echo SSH Keys loaded:;\n"; print "echo `ssh-add -l`\n"; } else { print "echo ERROR: found ssh-agent with pid=$ssh_pid but $found_sock SOCK files in /tmp - aborting\n"; exit; } } elsif( $found_pid > 1 ) { print "echo WARNING: Found $found ssh-agents running for user $user - aborting\n"; } else { print "echo No ssh-agent running, start one;\n"; system("ssh-agent -c"); # If "id_rsa" exists load it $keyfile = $ENV{"HOME"} . "/.ssh/id_rsa"; if( -e $keyfile ) { print "echo `ssh-add $keyfile`\n"; } }