Linux 期望+如何确定是否因为超时而期望中断?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9655384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:05:37  来源:igfitidea点击:

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

linuxshelltclkshexpect

提问by

The target of the following simple expect script is to get the hostnamename on the remote machine

以下简单期望脚本的目标是获取远程机器上的主机

Sometimes expect script fail to perform ssh to $IP_ADDRESS ( because remote machine not active , etc )

有时期望脚本无法对 $IP_ADDRESS 执行 ssh(因为远程机器不活动等)

so in this case the expect script will break after 10 second (timeout 10) , this is OK but......

所以在这种情况下,expect 脚本将在 10 秒后中断(超时 10),这没问题,但是......

There are two options

有两种选择

  1. Expect script perform ssh successfully , and performed the command hostname on the remote machine
  2. Expect script break because timeout was 10 seconds
  1. Expect脚本成功执行ssh,并在远程机器上执行了命令hostname
  2. 预计脚本中断,因为超时为 10 秒

On both cases expect will exit

在这两种情况下,expect 都会退出

  • in case of ssh successfully expect will break after 0.5-1 second but in case of bad ssh then it will break after 10 seconds
  • 如果 ssh 成功,预计会在 0.5-1 秒后中断,但如果 ssh 不好,则会在 10 秒后中断

but I don't know if expect script perform ssh successfully or not?

但我不知道expect脚本是否成功执行ssh?

is it possible to identify timeout process ? or to verify that expect ended because timeout?

是否可以识别超时过程?或验证期望因超时而结束?

Remark my Linux machine version - red-hat 5.1

备注我的 Linux 机器版本 - 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 we not have connection to the remote host

示例,以防我们没有连接到远程主机

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

采纳答案by Donal Fellows

To do sensible things on timeout, you need to tell expect what should happen:

要在超时时做明智的事情,您需要告诉 expect 应该发生什么:

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 I use exit 1when I hit an error. Your shell will be able to pick that up through $?, etc. (Without the 1argument, the exitcommand will cause the script to terminate “successfully”; the same happens if you drop off the bottom of the script.)

请注意上面exit 1我遇到错误时的使用方式。您的 shell 将能够通过$?等来获取它。(如果没有1参数,该exit命令将导致脚本“成功”终止;如果您从脚本底部退出,也会发生同样的情况。)

回答by kostix

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

并没有真正回答最初的问题,但是当您可以向 SSH 传递一个脚本来执行时,为什么还要与 SSH 交互呢?毕竟是壳。

I mean, just run:

我的意思是,只需运行:

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

and ssh will spawn the hostnamecommand remotely and connect its stdout to the stdout of the process which spawned ssh.

并且 ssh 将hostname远程生成命令并将其标准输出连接到生成的进程的标准输出ssh

Back to the point—thislooks like an example on how to bind an action to a timeout condition.

回到正题——看起来像是一个关于如何将操作绑定到超时条件的示例。