Dan Zaltsman
Dan Zaltsman

Reputation: 33

Expect script continues to run even the expect does not match anything

#!/usr/bin/expect 
spawn ssh [email protected] -o StrictHostKeyChecking=no
expect "5555:"
send "$password\n"
expect "% "
send "exit\n"

No matter what value wrote on expect "5555:" this command still working and I have ssh connection.

Upvotes: 2

Views: 1498

Answers (1)

Rachid K.
Rachid K.

Reputation: 5211

You should set a timeout and an action upon the timeout:

#!/usr/bin/expect
set timeout 5 
spawn ssh [email protected] -o StrictHostKeyChecking=no
expect {
   "5555:"  { }
   timeout { exit 2 }
}
send "$password\n"
expect "% "
send "exit\n"

Some extra info: the default timeout is 10 seconds. While expecting something, if the pattern has not been seen after $timeout seconds, the expect command without a "timeout" pattern simply returns and the script continues.

Upvotes: 3

Related Questions