期望脚本在 ping 响应后继续执行
Expect Script continues execute after ping response
你好,我想制作一个脚本来测试 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
}
}
}
首先,这个 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}
-- 大括号和 spaces。
接下来,那个 ping 命令不需要任何交互,所以不要生成它,只是 exec
它:
set ping_output [exec ping -c 2 -i 3 -W 1 $hostname]
然后检查 0%
的输出——注意,您不想匹配 100%
,所以在模式中添加前导 space:
if {[regexp { 0%} $ping_output]} {
spawn telnet $hostname
...
}
expect is an extension of tcl so all the Tcl commands 随时待命
你好,我想制作一个脚本来测试 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
}
}
}
首先,这个 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}
-- 大括号和 spaces。
接下来,那个 ping 命令不需要任何交互,所以不要生成它,只是 exec
它:
set ping_output [exec ping -c 2 -i 3 -W 1 $hostname]
然后检查 0%
的输出——注意,您不想匹配 100%
,所以在模式中添加前导 space:
if {[regexp { 0%} $ping_output]} {
spawn telnet $hostname
...
}
expect is an extension of tcl so all the Tcl commands 随时待命