views:

68

answers:

2

Hi,

I am using 'expect' to automate ssh password authentication. When I run the script in SunOS, I find the spawned ssh process gets killed once the below script is completed. This is not the case in Linux. How do we avoid it? Should we ignore the SIGCHLD signal somehow? Is there anyway to determine through this script if spawned process is successful and report error if any?

#!/usr/local/bin/expect -f

set password blah-blah
spawn ssh -NfL 8002:<test domain>:22 [email protected]
expect "* password:*"
send -- "$password\r"
send -- "\r"
expect EOF

-Karthik

+1  A: 

If you use ssh-keys, you won't need to code passwords in shell scripts.

You could even encrypt the key with a passphrase, and use ssh-agent to manage the key for you -- you unlock your key in the morning, start your tunnel, and then forget your key when you head to lunch, unlock your key in the afternoon, and forget it again when you go home at night. No on-disk magic gateway to remote machines.

sarnold
Thanks for the tip. But I don't think that answers my main question. I am looking for way to ask expect not to kill the spawned process.
Kartlee
This is the best solution - ssh keys are the best way to handle automated logins.
Douglas Leeder
@Kartlee, indeed, I don't know expect well, haven't used it in years. (For this same problem, until a co-worker told me to use ssh-keys. Hehe.)
sarnold
A: 

Instead of putting the ssh command in the background you could put the expect script into the background:

#!/usr/local/bin/expect -f

if {[fork] != 0} exit
disconnect

set password blah-blah
spawn ssh -NL 8002:localhost:22 [email protected]
expect {
    EOF {exit 1}
    "assword:" {}
}
send -- "$password\n"
send -- "\n"
expect EOF
wait

Works for me on Linux. At least for the setup phase, stopping it is more difficult. I had to kill -9 to stop the expect script. Which probably requires killing the ssh process as well.

Douglas Leeder
The original script works fine in Linux. I see a difference only in SunOS. Also the suggestion you gave now doesn't seem to work.
Kartlee
Does this work for you in SunOS?
Kartlee
Douglas' solution works fine for me on Solaris.
jlliagre
I'm afraid I don't have anywhere to usefully test it at the moment. It might require some tweaking to get it right.
Douglas Leeder
I've edited it a bit. But really - ssh keys are the way to go.
Douglas Leeder