shell, signals and trap question

Neil Watson tlug-neil-8agRmHhQ+n2CxnSzwYWP7Q at public.gmane.org
Thu Nov 1 14:43:08 UTC 2007


Consider the following code.

# Load private key into ssh-agent but, only for 5 minutes
eval "$(ssh-agent -t 300)" 2>1 1>/dev/null
# Add private keys to ssh-agent
ssh-add
# Kill ssh-agent on any exit
trap "eval \"$(ssh-agent -k)\" 2>1 1>/dev/null; exit" INT EXIT

# Main matter.  Perform tasks for each host in list
for h in $HOSTS
do

    # Remote command if from and to are empty
    if [ -z "$cfrom" ] && [ -z "$cto"  ]
    then
        ssh $h "$rcmd"

    # Else we want to copy
    else
        scp $cfrom ${h}:${cto}
    fi
done

exit 0


My test case is 'sshdo uptime'. Since I added the trap line I am prompted for
the ssh key passphrase for each iteration of the loop. This suggests to me that
the trap line is killing ssh-agent after each ssh session is finished. Why?

-- 
Neil Watson             | Debian Linux
System Administrator    | Uptime 13 days
http://watson-wilson.ca
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list