期待ping测试的脚本

时间:2019-11-01 18:03:42

标签: bash shell scripting expect

您好,我想制作一个脚本来测试ping并将输出结果保存到文件中。 感谢您的所有帮助。

#!/usr/bin/expect -f

    set IPaddress [lindex $argv 0]
    set fildes [open "ip.txt" r]
    set output [open "out.txt"]

while {[gets $fildes ip ] != -1} {

    set timeout 5
    spawn ping -c 3 $ip
    expect  {
        " 0%"   {puts "$ip Is Up"}
        " 100%" {puts "$ip Is Down"}
        }
}

我想把看跌期权“ $ ip Is Up”和“ $ ip Is Down”写入文件out.txt 然后我要计算有多少IP启用和有多少IP停用。 喜欢:

30 Ips Is up
20 Ips Is down

我还希望out.txt中的计数。

1 个答案:

答案 0 :(得分:1)

现在可以正常工作,我只需要添加“ w”,这是我的代码更新:

#!/usr/bin/expect -f

    set IPaddress [lindex $argv 0]
    set fildes [open "ip.txt" r]
    set output [open "out.txt" w]

while {[gets $fildes ip ] != -1} {

    set timeout 5
    spawn ping -c 3 $ip
    expect  {
        " 0%"   {puts $output "$ip Is Up"}
        " 100%" {puts $output "$ip Is Down"}
        }
}