使用tclsh中的有效负载ping服务器

时间:2013-01-03 05:33:52

标签: windows tcl ping

我需要测试我的远程服务器如何处理ping请求。我需要从我的窗口ping远程服务器,有效载荷为50 kb。我需要我的tcl脚本sholud生成20个此类ping请求,并且50 kb有效负载并行,因此在给定实例的服务器上将产生1 mb接收流量。这是ping测试的代码

proc ping-igp {} {
    foreach i {
        172.35.122.18
    } {
        if {[catch {exec ping $i -n 1 -l 10000} result]} {
            set result 0
        }
        if {[regexp "Reply from $i"  $result]} {
            puts "$i pinged"
        } else {
            puts "$i Failed"
        }
    }
}

2 个答案:

答案 0 :(得分:1)

如果要并行ping,那么可以使用open而不是exec,并使用fileevents从ping进程中读取。

使用open来ping具有两个并行进程的服务器的示例:

set server 172.35.122.18

proc pingResult {chan serv i} {
    set reply [read $chan]
    if {[eof $chan]} {
        close $chan
    }
    if {[regexp "Reply from $serv"  $result]} {
        puts "$serv number $i pinged"
    } else {
        puts "$serv number $i Failed"
    }
}

for {set x 0} {$x < 2} {incr $x} {
    set chan [open "|ping $server -n 1 -l 10000"]
    fileevent $chan readable "pingResult $chan {$server} $x"
}

有关详细信息,请参阅此页:http://www.tcl.tk/man/tcl/tutorial/Tcl26.html

答案 1 :(得分:0)

这是一段非常简单的代码,可以通过打开管道在后台执行ping操作。为此,将“{1}}的”文件名“的第一个字符设为open,当”filename“的其余部分被解释为命令行参数的Tcl列表时,就像在{ {1}}:

|

您还需要运行事件循环;这可能是微不足道的(你使用Tk)或者可能需要明确(exec)但不能集成到上面。但是你可以使用一个聪明的技巧来运行事件循环足够长的时间来收集所有结果:

proc doPing {host} {
    # These are the right sort of arguments to ping for OSX.
    set f [open "|ping -c 1 -t 5 $host"]
    fconfigure $f -blocking 0
    fileevent $f readable "doRead $host $f"
}

proc doRead {host f} {
    global replies
    if {[gets $f line] >= 0} {
        if {[regexp "Reply from $host" $result]} {
            # Switch to drain mode so this pipe will get cleaned up
            fileevent $f readable "doDrain $f"
            lappend replies($host) 1
            incr replies(*)
        }
    } elseif {[eof $f]} {
        # Pipe closed, search term not present
        lappend replies($host) 0
        incr replies(*)
        close $f
    }
}

# Just reads and forgets lines until EOF
proc doDrain {f} {
    gets $f
    if {[eof $f]} {close $f}
}

然后,只需查看vwait数组的内容即可获得所发生事件的摘要。