复制代码
- If (Lcase(Right(Wscript.FullName,11)) = "wscript.exe") Then
- CreateObject("WScript.Shell").Run("%Comspec% /C ""Title 适配器流量监测 &&Cscript.exe //NoLogo """ & Wscript.ScriptFullName & """"""), 3
- Wscript.Quit
- End If
- ConnectionID = "本地连接"
- Set ObjectNetwork = CreateObject("Wscript.Network")
- strComputer = ObjectNetwork.ComputerName
- '获取计算机名,必须用字符串,不能用.代替
- Set ObjectWSH = Wscript.createObject("Wscript.shell")
- Set ObjWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
- Wscript.Echo ConnectionID & " 信息"
- Set NetCards = ObjWMIService.ExecQuery("Select * from Win32_NetworkAdapter WHERE NetConnectionID = '" & ConnectionID & "'")
- If NetCards.Count<>0 Then
- For Each NetCard In NetCards
- WScript.Echo "适配器名: " & NetCard.Name & vbCrLf & "MAC 地址: " & NetCard.MACAddress
- NetCardName = NetCard.Name
- Next
- Else
- Wscript.Echo "网卡不存在."
- WScript.Quit(0)
- End If
- '获取用于上网的网卡信息
- Dim NetAdptName
- Dim TargetNetAdpt
- TargetNetAdpt = SelNetAdpt
- '监视网络
- '获取开始时接收的流量(字节)
- Dim BeginFlow
- BeginFlow = CurrentFlow
- Wscript.Echo "起始流量: " & CheckSize(BeginFlow)
- For i=1 to 1000
- '定义时间范围
- WScript.Sleep 3*1000
- TempFlow = CurrentFlow
- Wscript.Echo "使用流量: " & CheckSize(TempFlow - BeginFlow)
- Next
- WScript.Echo "Exit ..."
- WScript.Quit(0)
- Function SelNetAdpt
- Set ObjectFlows = objWMIService.InstancesOf("Win32_PerfRawData_Tcpip_NetworkInterface")
- k = ObjectFlows.Count
- If k < 1 Then Exit Function
- Redim NetAdptName(k)
- NetAdptName(0) = k
- k = 0
- For Each ObjectFlow In ObjectFlows
- k= k + 1
- NetAdptName(k) = ObjectFlow.Name
- Next
- Wscript.Echo ""
- Do
- For i = 1 To k
- Wscript.Echo i,NetAdptName(i)
- Next
- Wscript.Echo ""
- Wscript.Echo "请选择要监测的适配器:"
- TempNetAdpt = Int(WScript.StdIn.ReadLine())
- If (TempNetAdpt < 1) Or (TempNetAdpt > NetAdptName(0)) Then
- Wscript.Echo "选择有误,请重新选择。"
- Else
- Exit Do
- End If
- Loop
- SelNetAdpt = TempNetAdpt
- End Function
- Function CurrentFlow
- 'Set ObjectFlows = objWMIService.InstancesOf("Win32_PerfRawData_Tcpip_NetworkInterface")
- Set ObjectFlows = objWMIService.ExecQuery("Select * from Win32_PerfRawData_Tcpip_NetworkInterface Where Name = '"& NetAdptName(TargetNetAdpt) &"'")
- Dim CurrentFlowInt
- CurrentFlowInt = 0
- For Each ObjectFlow In ObjectFlows
- CurrentFlowInt =CurrentFlowInt + ObjectFlow.BytesReceivedPersec
- Next
- CurrentFlow = CurrentFlowInt
- End Function
- Function CheckSize(ByVal SZ)
- On Error Resume Next
- If SZ => 1073741824 Then
- SZ = FormatNumber(SZ/1073741824) & "GB"
- ElseIf SZ => 1048576 Then
- SZ = FormatNumber(SZ/1048576) & "MB"
- ElseIf SZ => 1024 Then
- SZ = FormatNumber(SZ/1024) & "KB"
- Else
- SZ = SZ & "字节"
- End If
- CheckSize=SZ
- On Error Goto 0
- End Function
欢迎光临 批处理之家 (http://bbs.bathome.net/) | Powered by Discuz! 7.2 |