如何从ping命令获取平均回复时间? [英] How to get average reply time from ping command?

查看:696
本文介绍了如何从ping命令获取平均回复时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我发现仅执行此命令就可以获取所有ping输出,但是也许我可以获取我的时间数据并在不使用字符串处理的情况下在变量中进行计算.

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)

推荐答案

我不建议脱壳到ping.exe然后解析输出.改用WMI:

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

这篇关于如何从ping命令获取平均回复时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆