【问题标题】:How to handle SSH failures in Expect script如何在 Expect 脚本中处理 SSH 失败
【发布时间】:2014-10-06 08:12:01
【问题描述】:

我有一个登录到设备列表并运行一系列命令的期望脚本。 一切正常,除非其中一台主机无法访问并且脚本刚刚退出。有没有办法让它跳过无法访问的主机并转移到剩余的设备上?

这是我脚本的主体。

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

【问题讨论】:

    标签: loops scripting automation tcl expect


    【解决方案1】:

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

    答案是,使用continue 命令代替exit 命令。

    【讨论】:

    • 就是这样!非常感谢:)
    猜你喜欢
    • 2022-10-19
    • 1970-01-01
    • 2016-09-20
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多