linux - expect + how to identify if expect break because time out? -


the target of following simple expect script hostname name on remote machine

sometimes expect script fail perform ssh $ip_address ( because remote machine not active , etc )

so in case expect script break after 10 second (timeout 10) , ok but......

there 2 options

  1. expect script perform ssh , , performed command hostname on remote machine
  2. expect script break because timeout 10 seconds

on both cases expect exit

  • in case of ssh expect break after 0.5-1 second in case of bad ssh break after 10 seconds

but don’t know if expect script perform ssh or not?

is possible identify timeout process ? or verify expect ended because timeout?

remark linux machine version - red-hat 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 in case not have connection remote host

 [testlinux]# expect -c  "$get_host_name_on_remote_machine"  spawn ssh 10.17.180.23  [testlinux]# echo $?  0 

to sensible things on timeout, need tell expect 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 above how use exit 1 when hit error. shell able pick through $?, etc. (without 1 argument, exit command cause script terminate “successfully”; same happens if drop off bottom of script.)


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -