linux – 期望如何识别是否因为超时而打破?

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

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

所以在这种情况下,期望脚本将在10秒后超时(超时10),这是可以的但……

有两种选择

> Expect脚本成功执行ssh,并在远程计算机上执行命令hostname
>期待脚本中断,因为超时为10秒

在这两种情况下,预计将退出

>如果ssh成功预期会在0.5-1秒之后中断,但是如果ssh不好则会在10秒后中断

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

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

备注我的Linux机器版本 – red-hat 5.1

期待脚本

[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`

我们没有连接到远程主机的示例

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

解决方法

你可以期待超时,某些版本需要-timeout就像-regex一样来测试超时的调用.

你期望声明可以成为

expect {
    ")?"    { send "yes\r"  ; exp_continue  }
    word:   { send $PASS\r}
    timeout { puts "Failed to SSH" }
       }

相关文章

Linux中的ARP防火墙主要用于防御ARP欺骗攻击,其效果取决于多...
insmod和modprobe加-f参数导致Invalid module format错误 这...
将ArchLinux安装到U盘 几个月前入门Arch的时候上网搜了不少安...
1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...