以下简单期望脚本的目标是获取远程计算机上的主机名
有时候期望脚本无法执行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