Board logo

标题: [问题求助] [已解决]VBS如何即时显示5秒内的网络流量? [打印本页]

作者: ygqiang    时间: 2011-10-11 12:53     标题: [已解决]VBS如何即时显示5秒内的网络流量?

本帖最后由 ygqiang 于 2011-10-11 23:00 编辑

流量监测.vbs

这个代码是统计的网络总流量,能否实现循环统计5秒内的网络流量??

多谢
  1. If (Lcase(Right(Wscript.FullName,11)) = "wscript.exe") Then
  2.    CreateObject("WScript.Shell").Run("%Comspec% /C  ""Title 适配器流量监测  &&Cscript.exe //NoLogo  """ & Wscript.ScriptFullName & """"""), 3
  3.    Wscript.Quit
  4. End If
  5. ConnectionID = "本地连接"
  6. Set ObjectNetwork = CreateObject("Wscript.Network")
  7. strComputer = ObjectNetwork.ComputerName
  8. '获取计算机名,必须用字符串,不能用.代替
  9. Set ObjectWSH = Wscript.createObject("Wscript.shell")
  10. Set ObjWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  11. Wscript.Echo ConnectionID & " 信息"
  12. Set NetCards = ObjWMIService.ExecQuery("Select * from Win32_NetworkAdapter WHERE NetConnectionID = '" & ConnectionID & "'")
  13. If NetCards.Count<>0 Then
  14.         For Each NetCard In NetCards
  15.                 WScript.Echo "适配器名: " & NetCard.Name & vbCrLf & "MAC 地址: " & NetCard.MACAddress
  16.                 NetCardName = NetCard.Name
  17.         Next
  18. Else
  19.         Wscript.Echo "网卡不存在."
  20.         WScript.Quit(0)
  21. End If
  22. '获取用于上网的网卡信息
  23. Dim NetAdptName
  24. Dim TargetNetAdpt
  25. TargetNetAdpt = SelNetAdpt
  26. '监视网络
  27. '获取开始时接收的流量(字节)
  28. Dim BeginFlow
  29. BeginFlow = CurrentFlow
  30. Wscript.Echo "起始流量: " & CheckSize(BeginFlow)
  31. For i=1 to 1000
  32. '定义时间范围
  33.                 WScript.Sleep 3*1000
  34.                 TempFlow = CurrentFlow
  35.                 Wscript.Echo "使用流量: " & CheckSize(TempFlow - BeginFlow)
  36. Next
  37. WScript.Echo "Exit ..."
  38. WScript.Quit(0)
  39. Function SelNetAdpt
  40.         Set ObjectFlows = objWMIService.InstancesOf("Win32_PerfRawData_Tcpip_NetworkInterface")
  41.         k = ObjectFlows.Count
  42.         If k < 1 Then Exit Function
  43.         Redim NetAdptName(k)
  44.         NetAdptName(0) = k
  45.         k = 0
  46.         For Each ObjectFlow In ObjectFlows
  47.                 k= k + 1
  48.                 NetAdptName(k) = ObjectFlow.Name
  49.         Next
  50.        Wscript.Echo ""
  51.         Do
  52.          For i = 1 To k
  53.                 Wscript.Echo i,NetAdptName(i)
  54.                Next
  55.                Wscript.Echo ""
  56.                Wscript.Echo "请选择要监测的适配器:"
  57.          TempNetAdpt = Int(WScript.StdIn.ReadLine())
  58.          If (TempNetAdpt < 1) Or (TempNetAdpt > NetAdptName(0)) Then
  59.                      Wscript.Echo "选择有误,请重新选择。"
  60.               Else
  61.                       Exit Do
  62.                End If
  63.          Loop
  64.          SelNetAdpt = TempNetAdpt
  65. End Function
  66. Function CurrentFlow
  67.         'Set ObjectFlows = objWMIService.InstancesOf("Win32_PerfRawData_Tcpip_NetworkInterface")
  68.         Set ObjectFlows = objWMIService.ExecQuery("Select * from Win32_PerfRawData_Tcpip_NetworkInterface Where Name = '"& NetAdptName(TargetNetAdpt) &"'")
  69.         Dim CurrentFlowInt
  70.         CurrentFlowInt = 0
  71.         For Each ObjectFlow In ObjectFlows
  72.                 CurrentFlowInt =CurrentFlowInt + ObjectFlow.BytesReceivedPersec
  73.         Next
  74.        CurrentFlow  = CurrentFlowInt
  75. End Function
  76. Function CheckSize(ByVal SZ)
  77.    On Error Resume Next
  78.    If SZ => 1073741824 Then
  79.        SZ = FormatNumber(SZ/1073741824) & "GB"
  80.    ElseIf SZ => 1048576 Then
  81.        SZ = FormatNumber(SZ/1048576) & "MB"
  82.    ElseIf SZ => 1024 Then
  83.        SZ = FormatNumber(SZ/1024) & "KB"
  84.    Else
  85.        SZ = SZ & "字节"
  86.    End If
  87.    CheckSize=SZ
  88.    On Error Goto 0
  89. End Function
复制代码

作者: slore    时间: 2011-10-11 16:24

说了重定义下BeginFlow即可。

                '定义时间范围
               
WScript.Sleep 5 * 1000
                TempFlow = CurrentFlow
                Wscript.Echo "使用流量: " & CheckSize(TempFlow - BeginFlow)
                BeginFlow = TempFlow
作者: fastslz    时间: 2011-10-11 19:19


http://www.bathome.net/thread-3198-1-1.html




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2