ping响应后,期望脚本继续执行

时间:2020-04-12 06:02:37

标签: expect telnet

您好,我想制作一个脚本,以在ping成功后测试ping并将继续执行命令。感谢您的所有帮助

在我的代码下面

set ip [10.10.10.1,10.10.10.2]
foreach hostname $ip {
    spawn ping -c 2 -i 3 -W 1 $hostname
    expect { "0%" {
            spawn telnet $hostname
            expect "*sername:"
            send "$userper\n"
            expect "*assword:"
            send "$passper\n"
            expect "#"
            send "exit\n"
            expect eof
        }
    }

}    

1 个答案:

答案 0 :(得分:0)

首先,此set ip [10.10.10.1,10.10.10.2]最有可能给您invalid command name "10.10.10.1,10.10.10.2"

要列出列表,请使用set ip {10.10.10.1 10.10.10.2}-大括号和空格。

接下来,该ping命令不需要任何交互,因此不要生成它,只需exec即可:

set ping_output [exec ping -c 2 -i 3 -W 1 $hostname]

然后检查0%的输出-注意,您不想匹配100%,因此请在模式中添加一个前导空格:

if {[regexp { 0%} $ping_output]} {
    spawn telnet $hostname
    ...
}

的扩展,因此all the Tcl commands供您使用

相关问题