Test ssh keys script - using expect instead

I had a problem with my previous script - often ssh-add would hang -executing echo continuously - which was marked as defunct :(

I figure I'd rewrite it in expect - to also make the script runnable outside of cron - and that proved a little bit challenging - but I got it solved, and figured I'd share it:

#!/usr/bin/expect
# may need TERM set to f.ex. xterm when calling from a script run via cron
exp_internal 0
set timeout 20
set key [lindex $argv 0]
spawn ssh-add $key
expect {
        -re "Enter passphrase" { send "\n" }
        eof { 
                catch wait reason
                set exit_status [lindex $reason 3]
        }
}
exit $exit_status

This expect script is then called in the script like this (instead of ssh-add):

/testsshkeys.expect $keyfile2check

 

1 comments on Test ssh keys script - using expect instead

  1. klavs
    Mon, 10/11/2010 - 11:37

    Oops - you need to change:

    -re "Enter passphrase" { send "\n" }

    to

    -re "Enter passphrase" {

    send "\n"

    exp_continue

    }