如何从 ping 命令获取平均回复时间?

How to get average reply time from ping command?

我需要知道如何在 vbs 中使用 ping 命令获得平均回复时间。

我发现只要执行这个命令我就能得到所有的 ping 输出,但也许我能得到我的时间数据并在变量中计算而不使用字符串处理。

Set objExec = objShell.Exec("ping -n 2 -w 1000 " & strTarget)
  strPingResults = LCase(objExec.StdOut.ReadAll)

我不建议对 ping.exe 进行炮击然后解析输出。改为使用 WMI:

target = 'somecomputer'
n = 2

Set wmi = GetObject("winmgmts://./root/cimv2")

qry = "SELECT * FROM Win32_PingStatus WHERE address='" & target & "'"

rspTime = 0
cnt = 0
For i = 1 To n
    For Each pingStatus In wmi.ExecQuery(qry)
        If Not IsNull(pingStatus.StatusCode) Or pingStatus.StatusCode = 0 Then
            rspTime = rspTime + pingStatus.ResponseTime
            cnt = cnt + 1
        End If
    Next
Next

If cnt > 0 Then
    WScript.Echo "Average response time: " & (rspTime / cnt)
Else
    WScript.Echo "Host unreachable"
End If