Eyt*_*tan 5 linux ksh tcl expect shell-scripting
以下简单期望脚本的目标是获取远程机器上的主机名
有时期望脚本无法对 $IP_ADDRESS 执行 ssh(因为远程机器不活动等)
所以在这种情况下,expect 脚本将在 10 秒后中断(超时 10),这没问题,但是......
有两种选择
在这两种情况下,expect 都会退出
但我不知道expect脚本是否成功执行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`
Run Code Online (Sandbox Code Playgroud)
示例,以防我们没有连接到远程主机
[TestLinux]# expect -c "$get_host_name_on_remote_machine"
spawn ssh 10.17.180.23
[TestLinux]# echo $?
0
Run Code Online (Sandbox Code Playgroud)
您可以预期超时,某些版本需要 -timeout 就像 -regex 来测试超时的调用。
你期望声明可能会变成
expect {
")?" { send "yes\r" ; exp_continue }
word: { send $PASS\r}
timeout { puts "failed to SSH" }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3337 次 |
| 最近记录: |