1

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 test@testdomain.com
expect "* password:*"
send -- "$password\r"
send -- "\r"
expect EOF

-Karthik

Kartlee
  • 1,129
  • 2
  • 19
  • 33

2 Answers2

1

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
  • 102,305
  • 22
  • 181
  • 238
  • 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 Aug 04 '10 at 09:37
  • This is the best solution - ssh keys are the best way to handle automated logins. – Douglas Leeder Aug 04 '10 at 09:38
  • @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 Aug 04 '10 at 10:47
1

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 test@testdomain.com
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
  • 52,368
  • 9
  • 94
  • 137