Expect +, how to determine if you expect a break because the timeout?

The purpose of the next simple wait script is to get the hostname on the remote computer

Sometimes it is expected that the script will not be able to execute ssh before $ IP_ADDRESS (since the remote machine is not active, etc.)

so in this case the wait for the script will break after 10 seconds (wait time 10), this is normal, but ......

There are two options.

  • Expect the script to execute ssh successfully and execute the hostname of the command on the remote computer
  • Expect a script break because the wait time is 10 seconds

In both cases, exit is expected.

  • in case of a successful wait, ssh breaks after 0.5-1 seconds, but in case of a bad ssh then it will break after 10 seconds.

but I don’t know if the script will expect ssh to execute successfully or not?

Is it possible to determine the wait time? or check that the wait is over because the timeout?

Remember my Linux machine version - Little Red Riding Hood 5.1

Expect script

[TestLinux]# get_host_name_on_remote_machine=`cat << EOF > set timeout 10 > spawn ssh $IP_ADDRESS > expect { > ")?" { send "yes\r" ; exp_continue } > > word: {send $PASS\r} > } > expect > {send "hostname\r"} > expect > {send exit\r} > expect eof > EOF` 

Example if we do not have a connection to a remote host

  [TestLinux]# expect -c "$get_host_name_on_remote_machine" spawn ssh 10.17.180.23 [TestLinux]# echo $? 0 
+4
source share
2 answers

To do sensible things in a timeout, you need to say what should happen:

 set timeout 10 expect { ")?" { send "yes\r" ; exp_continue } "word:" { send "$PASS\r" } timeout { puts "timed out during login"; exit 1 } } set timeout -1 ; # Infinite... expect ">" { send "hostname\r" } expect ">" { send "exit\r" } expect eof exit 

Notice how I use exit 1 on error. Will your shell be able to select this through $? etc. (Without argument 1 , the exit will cause the script to complete “successfully”, the same thing will happen if you lower the bottom of the script.)

+14
source

Not really answering the original question, but why are you talking to SSH interactively when you can just pass it a script to execute? This is the shell after all.

I mean, just run:

 ssh user@host '/usr/bin/hostname' 

and ssh will output the hostname command remotely and connect its stdout to the process step that generated ssh .

Back to the point, this> looks like an example of how to bind an action to a timeout condition.

+4
source

Source: https://habr.com/ru/post/1400838/


All Articles