How to get average reply time from ping command?

点点圈 提交于 2019-12-08 13:22:26

问题


I need to know how to get average reply time from using ping command in the vbs.

I found out that I can get all ping output just executing this command but may be I can just get my time data and calculate in variable without using string handling.

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

回答1:


I would not recommend shelling out to ping.exe and then parsing the output. Use WMI instead:

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


来源:https://stackoverflow.com/questions/58812916/how-to-get-average-reply-time-from-ping-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!