Sign in to follow this  
Followers 0
omnomnom

Need help monitoring uploaded bytes using netstat

8 posts in this topic

#1 ·  Posted (edited)

So i got this code about two months ago from these forums, i dont understand much of it at all. First question - Is there any way of making this greatly simplified so i could edit it myself?

second question - this gets the received bytes, but not the sent bytes... Could someone please help me to get it to add the received and the sent bytes together before displaying it? (i need a lot of help because it is hard for me to understand)

I really appreciate it, thanks.

#include <Constants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $str[20], $trimmed = 0, $alltrimmed = 0, $stout = ""
$Form1 = GUICreate("Form1", 321, 70, 355, 477)
$Label1 = GUICtrlCreateLabel("Received bytes", 8, 24, 117, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Input1 = GUICtrlCreateInput("0", 154, 24, 153, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)

AdlibRegister("GetInfo", 1000)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func GetInfo()
    $cmd = Run(@comspec & " /c netstat -e", @SystemDir, @SW_HIDE,  $STDIN_CHILD & $STDOUT_CHILD)
    Sleep(500)
    $line = StdoutRead($cmd)
    ;getting from STDOUT only number of received bytes and update control whith this info:
    $str = StringSplit($line,@CR)
    For $i = 1 to $str[0]-1 Step 2
        If $str[$i] <> "" And $i = 5 Then
            $strn = StringSplit($str[$i],"")
            $string = $str[$i]
            For $n = 1 to $strn[0] Step 1
                If StringIsDigit($strn[$n]) And $trimmed = 0 Then
                    $alltrimmed = 1
                    $stout = $stout & $strn[$n]
                ElseIf Not StringIsDigit($strn[$n]) And $alltrimmed = 1 Then
                    $trimmed = 1
                EndIf
            Next
                GUICtrlSetData($Input1, int($stout))
                $stout = ""
                $trimmed = 0
                $alltrimmed = 0

        EndIf
    Next

    EndFunc
Edited by omnomnom

Share this post


Link to post
Share on other sites



Here's a trimmed down version:

#include <Constants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>

$Form1 = GUICreate("Form1", 321, 70, 355, 477)
$Label1 = GUICtrlCreateLabel("Bytes I/O", 8, 24, 117, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Input1 = GUICtrlCreateInput("0", 154, 24, 153, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)

AdlibRegister("GetInfo", 1000)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
WEnd

Func GetInfo()
    Local $cmd = Run(@comspec & " /c netstat -e", @SystemDir, @SW_HIDE, $STDIN_CHILD & $STDOUT_CHILD)
    Sleep(500)
    Local $line = StdoutRead($cmd)
    ; getting from STDOUT only number of bytes received and bytes sent and update control whith this info:
    Local $bytes = StringRegExp($line, "(?m)^[^\d]+\s+(\d+)\s+(\d+)", 3)
    GUICtrlSetData($Input1, $bytes[0] + $bytes[1])
; run from SciTe to see that working, but remove this line for compile and production
ConsoleWrite('Bytes received: ' & $bytes[0] & ' bytes sent: ' & $bytes[1] & @LF)
EndFunc

Hope it will work for you.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Thanks a lot, with a little effort i will be able to hopefully understand it.

firstly, i get this error

C:\Users\*\*\*\*\*\internetinfo.au3 (27) : ==> Subscript used with non-Array variable.:

GUICtrlSetData($Input1, $bytes[0] + $bytes[1])

GUICtrlSetData($Input1, $bytes^ ERROR

secondly, i dont understand this line

Local $bytes = StringRegExp($line, "(?m)^[^\d]+\s+(\d+)\s+(\d+)", 3)

or to be more specific this line-

"(?m)^[^\d]+\s+(\d+)\s+(\d+)"

I really appreciate your help, its a great aid in helping me learn autoit x)

Edited by omnomnom

Share this post


Link to post
Share on other sites

Ok it's due to the fact that I used my XP Pro SP3 x86 French version of netstat. Your OS is likely to return something different. Can you please run the following in a command prompt:

C:\> netstat -e

and copy paste the output?

Are you running the script as admin?


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Interface Statistics

Received Sent

Bytes 295483551 61277658

Unicast packets 386982 292407

Non-unicast packets 47841 3876

Discards 0 0

Errors 0 45

Unknown protocols 0

This is on vista 32bit business. However this script will be used on windows server 2003.

Share this post


Link to post
Share on other sites

Ummm, I wonder why.

Try this version (forcibly runs as admin) and forces recognition of the word Bytes:

#RequireAdmin
#include <Constants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>

$Form1 = GUICreate("Form1", 321, 70, 355, 477)
$Label1 = GUICtrlCreateLabel("Ethernet Bytes I/O", 8, 24, 140, 20)
GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif")
$Input1 = GUICtrlCreateInput("0", 154, 24, 153, 21, BitOR($ES_AUTOHSCROLL,$ES_READONLY))
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)

AdlibRegister("GetInfo", 1000)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
WEnd

Func GetInfo()
    Local $cmd = Run(@comspec & " /c netstat -e", @SystemDir, @SW_HIDE, $STDOUT_CHILD)
    Sleep(500)
    Local $line = StdoutRead($cmd)
    ; getting from STDOUT only number of bytes received and bytes sent and update control whith this info:
    Local $bytes = StringRegExp($line, "(?m)^Bytes\s+(\d+)\s+(\d+)", 3)
    GUICtrlSetData($Input1, $bytes[0] + $bytes[1])
; run from SciTe to see that working, but remove this line for compile and production
ConsoleWrite('Bytes received: ' & $bytes[0] & ' bytes sent: ' & $bytes[1] & @LF)
EndFunc

The pattern (?m)^Bytes\s+(\d+)\s+(\d+) breaks dow as follows:

(?m) forces the co-called "multiline" mode

^ aligns on either start of subject string, or on start of some line (this is our case here)

Bytes matches itself, i.e. the string "Bytes"

\s+ matches the longest possible string of one or more whitespaces (blank, tabs, ...)

(\d+) matches the longest possible string of one or more decimal digits and the outer parenthesis make a capture of that, stored as first item in the result array (bytes received)

\s+ matches the longest possible string of one or more whitespaces (blank, tabs, ...)

(\d+) matches the longest possible string of one or more decimal digits and the outer parenthesis make a capture of that, stored as second item in the result array (bytes sent)

We then add both numbers and display.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Thanks, when i ran it the first time it didnt not work with that error (using the code you provided) however i ran it a second time and it has worked since. Thanks a lot, i appreciate it!

Share this post


Link to post
Share on other sites

The volatile error may be due to the command shell + netstat not being "just ready" and a busy machine. That's why you have a Sleep(500) after the Run(...) command. Once run once, it should keep on running fast as everything will be in cache. If you experience such error too frequently, come again and someone will show you how to overcome the situation.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

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