Loops 如何在Expect脚本中处理SSH故障

Loops 如何在Expect脚本中处理SSH故障,loops,scripting,automation,tcl,expect,Loops,Scripting,Automation,Tcl,Expect,我有一个expect脚本,它可以登录到设备列表并运行一系列命令。 除了其中一台主机无法访问和脚本刚刚退出外,其他一切都正常工作。有没有办法让它跳过无法访问的主机并转移到其余的设备 这是我剧本的主体 foreach host $hosts { spawn -noecho /usr/bin/ssh user@$host set timeout 10 expect { "assword:" { send [string trimrigh

我有一个expect脚本,它可以登录到设备列表并运行一系列命令。 除了其中一台主机无法访问和脚本刚刚退出外,其他一切都正常工作。有没有办法让它跳过无法访问的主机并转移到其余的设备

这是我剧本的主体

foreach host $hosts {
    spawn -noecho /usr/bin/ssh user@$host
    set timeout 10
    expect {
        "assword:"              { send [string trimright "$pwd" "\r"]   }
        "No route*"             {puts "Host error -> $expect_out(buffer)";exit}
        "Could not resolve*"    {puts "Host error -> $expect_out(buffer)";exit}
    }
    expect "#"
    send "term len 0\r"
    expect "#"
    send "show version\r"
    expect "#"
    send "exit\r"
    expect eof
}
下面是我得到的:

.
. <output of reachable device - R1> 
.
Connection to R1 closed by remote host.
Connection to R1 closed.
ssh: Could not resolve hostname R2: Name or service not known
Host error -> ssh: Could not resolve hostname R1: Name or service not known
。
.  
.
远程主机已关闭与R1的连接。
与R1的连接已关闭。
ssh:无法解析主机名R2:名称或服务未知
主机错误->ssh:无法解析主机名R1:名称或服务未知

鉴于Expect本质上是TCL语言的扩展,您的问题实际上可以归结为“如何在TCL早期结束循环迭代?”


答案是,使用
continue
命令,而不是
exit
命令。

这样做!非常感谢:)