Sign in to follow this  
Followers 0
jornable

Obtaining network traffic info

8 posts in this topic

Hi.

I'd like to log the Internet traffic (upload/download in MB) i am causing using a mobile connection.

The connection works like a mobile phone, it uses a USB modem to connect to UMTS/HSDPA network.

i have searched some different topics in this forum, and i actually found something related:

http://www.autoitscript.com/forum/index.ph...twork++activity

What i need to know is how to obtain the information of how much data has been received/sent

(I'd like saving this information to a text file or display it with a GUI, but i think i can figure that out by myself).

Thanks in advance!

Share this post


Link to post
Share on other sites



Same idea as in the thread... Just don't monitor it over time-

#include <GUIConstants.au3>

GUICreate("Based on Lod3n's Bandwidth Monitor:",220,100,0,0,-1,$WS_EX_TOOLWINDOW)

$label1 = GUICtrlCreateLabel ( "Waiting for data...", 10, 5,200,20)
$label2 = GUICtrlCreateLabel ( "Waiting for data...", 10, 50,200,20)

GUISetState ()

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

while 1
    $colItems = $objWMIService.ExecQuery("SELECT BytesReceivedPersec,BytesSentPersec FROM Win32_PerfRawData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) then
        For $objItem In $colItems
            $in = $objItem.BytesReceivedPersec
            $out = $objItem.BytesSentPersec
    
            $intext = "Bytes In: " & $in & @CRLF
            $outtext = "Bytes Out: " & $out &@CRLF

            GUICtrlSetData ($label1,$intext)
            GUICtrlSetData ($label2,$outtext)
            ExitLoop ; I only care about the first network adapter, yo
        Next
    EndIf
    sleep(100) 
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

Share this post


Link to post
Share on other sites

ok, thank you very much!

Share this post


Link to post
Share on other sites

Do you know how to specify which adapter to monitor?

Share this post


Link to post
Share on other sites

You need to find out the exact adapter name but this works...

#include <GUIConstants.au3>
#include <WindowsConstants.au3>

GUICreate("Based on Lod3n's Bandwidth Monitor:",220,100,0,0,-1,$WS_EX_TOOLWINDOW)

$label1 = GUICtrlCreateLabel ( "Waiting for data...", 10, 5,200,20)
$label2 = GUICtrlCreateLabel ( "Waiting for data...", 10, 50,200,20)

GUISetState ()

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

while 1
    $colItems = $objWMIService.ExecQuery("SELECT BytesReceivedPersec,BytesSentPersec FROM Win32_PerfRawData_Tcpip_NetworkInterface", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) then
        For $objItem In $colItems
            If $objItem.Name = "ADAPTER NAME HERE" Then ;MUST PUT ADAPTER NAME HERE
                $in = $objItem.BytesReceivedPersec
                $out = $objItem.BytesSentPersec
   
                $intext = "Bytes In: " & $in & @CRLF
                $outtext = "Bytes Out: " & $out &@CRLF

                GUICtrlSetData ($label1,$intext)
                GUICtrlSetData ($label2,$outtext)
            EndIf
        Next
    EndIf
    sleep(100)
    If GUIGetMsg() = $GUI_EVENT_CLOSE Then ExitLoop
WEnd

Share this post


Link to post
Share on other sites

Using API:

; $iFlag:
; 1 - The number of bytes transmitted through this connection or link.
; 2 - The number of bytes received through this connection or link.
; 3 - Total
; 4 - The amount of time, in milliseconds, that the connection or link has been connected.

$stat = _RASConStat(4)

MsgBox(0, "Connect Statistics", _
        StringFormat("Connection duration:\n\nIn milliseconds: %s\nIn seconds: %.2f\nIn minutes: %.2f", $stat, $stat / 1000, ($stat / 1000) / 60))

Func _RASConStat($sFlags = 1)
    Local Const $MAX_PATH = 260
    Local Const $RAS_MaxDeviceType = 16
    Local Const $RAS_MaxEntryName = 256
    Local Const $RAS_MaxDeviceName = 128

    $tRASCONN = DllStructCreate("dword dwSize;hwnd hRasConn;char szEntryName[" & $RAS_MaxEntryName + 1 & "];" & _
                                "char szDeviceType[" & $RAS_MaxDeviceType + 1 & "];" & _
                                "char szDeviceName[" & $RAS_MaxDeviceName + 1 & "];" & _
                                "char szPhonebook["  & $MAX_PATH & "];" & _
                                "dword dwSubEntry;byte guidEntry[16];dword dwFlags;byte luid[8]")

    $tRAS_STATS = DllStructCreate("dword dwSize;dword dwBytesXmited;dword dwBytesRcved;dword dwFramesXmited;" & _
                                  "dword dwFramesRcved;dword dwCrcErr;dword dwTimeoutErr;dword dwAlignmentErr;" & _
                                  "dword dwHardwareOverrunErr;dword dwFramingErr;dword dwBufferOverrunErr;" & _
                                  "dword dwCompressionRatioIn;dword dwCompressionRatioOut;dword dwBps;dword dwConnectDuration")

    $iCntByte = DllStructCreate("dword")
    $iCntConn = DllStructCreate("dword")

    DllStructSetData($iCntByte, 1, DllStructGetSize($tRASCONN))
    DllStructSetData($tRASCONN, "dwSize", DllStructGetSize($tRASCONN))
    DllStructSetData($tRAS_STATS, "dwSize", DllStructGetSize($tRAS_STATS))

    $aRet = DllCall("rasapi32.dll", "int", "RasEnumConnections", _
                                            "ptr", DllStructGetPtr($tRASCONN), _
                                            "ptr", DllStructGetPtr($iCntByte), _
                                            "ptr", DllStructGetPtr($iCntConn))
    If $aRet[0] Then Return SetError(2, $aRet[0], -1)
    If DllStructGetData($iCntConn,1) < 1 Then Return SetError(1, 0, 0) ;Error: not opened connections

    $aRet = DllCall("rasapi32.dll", "int", "RasGetConnectionStatistics", _
                                            "hwnd", DllStructGetData($tRASCONN, "hRasConn"), _
                                            "ptr", DllStructGetPtr($tRAS_STATS))
    If $aRet[0] Then Return SetError(3, $aRet[0], -1)
    
    Local $iResult = 0
    
    Switch $sFlags
        Case 1
            $iResult = DllStructGetData($tRAS_STATS, "dwBytesXmited")
        Case 2
            $iResult = DllStructGetData($tRAS_STATS, "dwBytesRcved")
        Case 3
            $iResult = DllStructGetData($tRAS_STATS, "dwBytesXmited") + DllStructGetData($tRAS_STATS, "dwBytesRcved")
        Case 4
            $iResult = DllStructGetData($tRAS_STATS, "dwConnectDuration")
    EndSwitch
    
    Return $iResult
EndFunc ; ==> _RASConStat

Share this post


Link to post
Share on other sites

This is what I'm working on but a little more detailed.

I'm trying to find a way to get the protocol for the traffic, and it would be nice to get the port too is that possible?

This should pick up traffic being routed through the machine and not just local traffic right?

Thanks,

Kenny


My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites

FYI, by protocol I don't mean TCP/UDP

I mean http, ftp etc.


My Contributions _StringMultiReplace PC Builders Console - Secure PDF Creator - Cisco VPN Installer MS DNS Server Backup Script - MS DHCP Backup Script IT Admin Console - Toggle Admin Mode - MyMovies-Add Discs Script - IT Help Desk and System Information Tool - Set On Lid Close Power Option - Streaming Media Server & Website "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0