Steve
Steve

Reputation: 347

Expect: How to handle spawn id exp4 not open error

My expect script:

foreach host $hostlist {
  puts $host
  set timeout 3
  spawn ssh -t -q -o StrictHostKeychecking=no "$user\@$host"
  expect {
    -re "closed by remote host" { exp_continue }
    -re "RSA key fingerprint" { send "yes\r"; exp_continue }
    -re "(P|p)assword: " { send "$pass\r"; exp_continue }
    -re $prompt { send "$cmd\r" }
    eof
  }
...

is throwing this error and exits right away.

spawn ssh -t -q -o StrictHostKeychecking=no [email protected]
**expect: spawn id exp4 not open**
    while executing
"expect -re $prompt"
    ("foreach" body line 12)
    invoked from within
"foreach host $hostlist {
  puts $host
  set timeout 3
  spawn ssh -t -q -o StrictHostKeychecking=no "$user\@$host"
  expect {
    -re "closed by rem..."
    (file "./a" line 36)

whenever the ssh login attempt encounters some problem on a host like:

kex_exchange_identification: Connection closed by remote host

I tried to put various "expect" regexps but couldn't make it continue to the next host but it abruptly exits.

Anyone know of a trick on how to make it not exit but move onto the next host?

If it's not possible using tcl/expect, would it be possible using the expect module with Python or Perl?

Thank you for your help in advance!

Upvotes: 0

Views: 340

Answers (1)

glenn jackman
glenn jackman

Reputation: 246764

Change

    -re "closed by remote host" { exp_continue }

to

    -re "closed by remote host" { continue }

You want to go to the next foreach iteration, not loop within this expect command to look for the next pattern.

Upvotes: 1

Related Questions