VBS监控网络流量

标签: , , , ,

VBS监控网络流量,对于晚上挂迅雷等,可能有点用处。

On Error Resume Next
Dim ObjectWSH, ObjectWMI, TempFlow

Const FlowCount = 10240 '单位流量,10KB,10240字节
Const TimeCount = 30000 '单位时间,30秒,30000毫秒
'这里根据实际情况修改

TempFlow = 0

Set ObjectWSH = CreateObject("Wscript.Shell")
Set ObjectWMI = Getobject("Winmgmts:\\.\")

Set NetCards = ObjectWMI.ExecQuery("Select * from Win32_PerfRawData_Tcpip_NetworkInterface")

If NetCards.count<>0 Then
    For Each NetCard In NetCards
        If (NetCard.BytesReceivedPersec > TempFlow) Then
            TempFlow = NetCard.BytesReceivedPersec
            NetCardName = NetCard.Name
        End If
    Next
    WScript.Echo "Name: " & NetCardName
    Else
    Wscript.Echo "Not found the specified network device ..."
    WScript.Quit(0)
End If
'获取用于上网的网卡信息(最大流量的网卡)

Do
    WScript.Echo ""
    WScript.Sleep 1000
    '这个延时貌似是必须的?
    
    CurrentFlow
    BeginFlow = CurrentFlow
    Wscript.Echo Now & ", Flow: " & BeginFlow & " Bytes"
    '获取开始时接收的流量(字节)

    WScript.Sleep TimeCount
    '定义时间范围

    CurrentFlow
    EndFlow = CurrentFlow
    Wscript.Echo Now & ", Flow: " & EndFlow & " Bytes"
    '获取结束时接收的流量(字节)

    Result = EndFlow-BeginFlow
    If (Result < FlowCount) Then
        '10240即10KB,当30秒(上面的时间范围)内流量小于10KB就关闭系统
        WScript.Echo "Shutdown ..."
        ObjectWSH.Run "shutdown.exe -s -t 0", 0, TRUE
        WScript.Quit(0)
    End If
    '计算结果并比较
Loop
WScript.Quit(0)

Function CurrentFlow()
    Set ObjectFlows = ObjectWMI.InstancesOf("Win32_PerfRawData_Tcpip_NetworkInterface")
    For Each ObjectFlow In ObjectFlows
        If ObjectFlow.Name=NetCardName Then
            CurrentFlow = ObjectFlow.BytesReceivedPersec
        End If
    Next
End Function

原文链接:http://www.enun.net/?p=147

随机文章:

  1. JavaScript调用VBS中的InputBox和MsgBox函数
  2. VBS调用WMI监视注册表变动
  3. 批处理技术内幕:随机数%RANDOM%
  4. iPad 2 完美越狱
  5. Ubuntu查看系统运行时间

留下回复