#!/bin/sh # debug is off by default. Set AD1_SSH_DEBUG debug=${AD1_SSH_DEBUG:-} env=$HOME/.ssh/env # # check wether # # $env exists # $env is sane # environment is set # environment is sane # agent is running # agent holds identity # ssh-add -l > /dev/null 2>&1 if test $? != 2 then # no need to add new agent exit 0 fi if test -f $env then . $env > /dev/null # check freshness of $env pid=`pgrep -n -U $LOGNAME ssh-agent` if test \( -n "${SSH_AGENT_PID}" \) -a \( "${SSH_AGENT_PID}" = "${pid}" \) then test -n "$debug" && echo >&2 running agent found in $env else test -n "$debug" && echo >&2 starting agent ssh-agent > $env 2>/dev/null . $env > /dev/null fi fi # TODO : do something sane in case $env does not exist if ssh-add -l | grep 'no identities' > /dev/null then # test -n "$debug" && echo >&2 adding identity to agent # test -n "$debug" && echo >&2 you should now add an identity to your agent echo >&2 no identities in agent. aargh # exec ssh-add fi if test \( -n "$SSH_AUTH_SOCK" \) -a \( -n "$SSH_AGENT_PID" \) then echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK; export SSH_AUTH_SOCK;" echo "SSH_AGENT_PID=$SSH_AGENT_PID; export SSH_AGENT_PID;" fi exit 0 POD=<<'EOPOD' =pod =head1 NAME ssh-attach - attach to running ssh-agent =head1 SYNOPSIS eval `ssh-attach` =head1 DESCRIPTION B attaches to a running ssh-agent. If no agent is found, it creates one. It uses the file C<~/.ssh/env> to store state. =head1 ENVIRONMENT Set AD1_SSH_DEBUG to '1' to enable debugging. Set it to '' to disable this. Per default, debugging is off. =head1 BUGS B does i yet behave sanely in case no running agent is found. To bootstrap this tool, run % killall ssh-agent % ssh-agent > .ssh/env % . .ssh/env % ssh-add . =head1 FILES ~/.ssh/env: used to store ssh-agent environment. =head1 SEE ALSO http://www.cvrti.utah.edu/~dustman/no-more-pw-ssh/ http://www.interactive.co.uk/~rjc/FreeBSD/ssh_agent.html =head1 VERSION $Id: ssh-attach,v 1.10 2004/01/25 16:18:08 joostvb Exp $ , under version control on $Source: /var/cvs/ad1810.com/packages/ad1810-ssh/script/ssh-attach,v $ =head1 COPYRIGHT Copyright 2003 Joost van Baal This script is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. A copy of the GNU General Public License is available from http://www.gnu.org/copyleft/gpl.html, or write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. =head1 THANKS Rudi Sluijtman, Joost Kooij, Arjan de Vet, Jos Backus (and possibly others), for writing an earlier (non-free) ssh-attach implementation for Origin in Eindhoven, The Netherlands. =head1 AUTHOR Joost van Baal =cut EOPOD