Jump to content

Help with StdoutRead


Recommended Posts

#include <Constants.au3>

Local $cmd = Run(@comspec & " /c netstat -e", @SystemDir, @SW_SHOW, $STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
    $line = StdoutRead($cmd)
    If @error Then ExitLoop
    MsgBox(0, "STDOUT read:", $line)
Wend

How to get from STDOUT only info about received bytes?

And why there are 2 msgboxes, with empty first?

_____________________________________________________________________________

Link to comment
Share on other sites

How to get from STDOUT only info about received bytes?

I don't know what do you mean; what extra info do you get and you don't want to?

After the script exits the loop, $line contains whatever was received. If it doesn't and some content is missing you can change the following line: $line = StdoutRead($cmd)

to $line &= StdoutRead($cmd)

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

I don't know what do you mean; what extra info do you get and you don't want to?

After the script exits the loop, $line contains whatever was received. If it doesn't and some content is missing you can change the following line: $line = StdoutRead($cmd)

to $line &= StdoutRead($cmd)

Hi,

maybe another solution for getting received bytes/sec:

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
    For $objItem In $colItems
      $bytesrec = $objItem.BytesReceivedPersec
    Next
EndIf
MsgBox (0,"", "received Bytes per sec: " & $bytesrec)

If you have more then 1 NIC, you could add an if before line $bytesrec =

like: If StringInStr ($objitem.Name, "VMware Accelerated AMD PCNet Adapter") <> 0 Then $bytesrec = ....

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

What wrong with this:

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

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = 3
$strComputer = @ComputerName

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

$Form1 = GUICreate("Form1", 410, 78, 279, 557)
$Label1 = GUICtrlCreateLabel("Bytes/sec", 8, 16, 63, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$rec = GUICtrlCreateInput("", 80, 16, 113, 21)
GUISetState(@SW_SHOW)


While 1
Sleep(10)
If IsObj($colItems) then
    For $objItem In $colItems
      $bytesrec = $objItem.BytesReceivedPersec
      GUICtrlSetData($rec, $bytesrec)
  Next
EndIf

WEnd

I see only zero in $rec

Edited by Vitas

_____________________________________________________________________________

Link to comment
Share on other sites

What wrong with this:

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

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = 3
$strComputer = @ComputerName

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_Tcpip_NetworkInterface", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

$Form1 = GUICreate("Form1", 410, 78, 279, 557)
$Label1 = GUICtrlCreateLabel("Bytes/sec", 8, 16, 63, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
$rec = GUICtrlCreateInput("", 80, 16, 113, 21)
GUISetState(@SW_SHOW)


While 1
Sleep(10)
If IsObj($colItems) then
    For $objItem In $colItems
      $bytesrec = $objItem.BytesReceivedPersec
      GUICtrlSetData($rec, $bytesrec)
  Next
EndIf

WEnd

I see only zero in $rec

Time-based perf data (i.e. anythin "persec") is supposed to be referenced using a refresh object:
#include <GUIConstantsEx.au3>

Global $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
Global $colItems, $strComputer = @ComputerName
Global $Output, $oTcpipIntfc, $oItem

Global $Form1 = GUICreate("Form1", 400, 400)
Global $Edit1 = GUICtrlCreateEdit("Bytes/sec:", 10, 10, 380, 340)
Global $Button1 = GUICtrlCreateButton("Quit", 150, 360, 100, 30)
GUISetState(@SW_SHOW)

Global $oWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
Global $oRefresher = ObjCreate("WbemScripting.Swbemrefresher")
Global $oTcpipNetworkIntfc = $oRefresher.AddEnum($oWMIService, "Win32_PerfRawData_Tcpip_NetworkInterface" ).ObjectSet
Global $iTimer = TimerInit()

If IsObj($oWMIService) And IsObj($oRefresher) And IsObj($oTcpipNetworkIntfc) Then
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button1
                Exit
            Case Else
                If TimerDiff($iTimer) >= 1500 Then
                    $Output = "Computer: " & $strComputer & @CRLF
                    $Output &= "==========================================" & @CRLF
                    $oRefresher.Refresh
                    $n = 0
                    For $oItem In $oRefresher
                        For $oTcpipIntfc In $oItem.ObjectSet
                            $bytesrec = $oTcpipIntfc.BytesReceivedPersec
                            If $bytesrec < 0 Then $bytesrec += 2^32 ; Correct UINT32 to INT32 error for bit 32
                            $Output &= $n & ": " & $bytesrec & @CRLF
                            $n += 1
                        Next
                    Next
                    ControlSetText($Form1, "", $Edit1, $Output)
                    $iTimer = TimerInit()
                EndIf
        EndSwitch
    WEnd
Else
    MsgBox(16, "Error", "Failed to initialize objects.")
    Exit
EndIf

The interesting thing is that while I seem to get valid data with that, it is giving me total bytes received (constantly counting up) vice bytespersec?

>_<

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

#include <GUIConstantsEx.au3>


Global $wbemFlagReturnImmediately = 0x10, $wbemFlagForwardOnly = 0x20
Global $colItems, $strComputer = @ComputerName
Global $Output, $oTcpipIntfc, $oItem

Global $Form1 = GUICreate("Form1", 400, 400)
Global $Edit1 = GUICtrlCreateEdit("Bytes/sec:", 10, 10, 380, 340)
Global $Button1 = GUICtrlCreateButton("Quit", 150, 360, 100, 30)
GUISetState(@SW_SHOW)

Global $oWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
Global $oRefresher = ObjCreate("WbemScripting.Swbemrefresher")
Global $oTcpipNetworkIntfc = $oRefresher.AddEnum($oWMIService, "Win32_PerfRawData_Tcpip_NetworkInterface" ).ObjectSet
Global $iTimer = TimerInit()

If IsObj($oWMIService) And IsObj($oRefresher) And IsObj($oTcpipNetworkIntfc) Then
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Button1
                Exit
            Case Else
                If TimerDiff($iTimer) >= 1500 Then
                    $Output = "Computer: " & $strComputer & @CRLF
                    $Output &= "==========================================" & @CRLF
                    $oRefresher.Refresh
                    $n = 0
                    For $oItem In $oRefresher
                        For $oTcpipIntfc In $oItem.ObjectSet
                            $bytesrec = $oTcpipIntfc.BytesReceivedPersec
                            If $bytesrec < 0 Then $bytesrec += 2^32 ; Correct UINT32 to INT32 error for bit 32
                            $Output &= $n & ": " & $bytesrec & @CRLF
                            $n += 1
                        Next
                    Next
                    ControlSetText($Form1, "", $Edit1, $Output)
                    $iTimer = TimerInit()
                EndIf
        EndSwitch
    WEnd
Else
    MsgBox(16, "Error", "Failed to initialize objects.")
    Exit
EndIf

: ==> The requested action with this object has failed.:

Global $oTcpipNetworkIntfc = $oRefresher.AddEnum($oWMIService, "Win32_PerfRawData_Tcpip_NetworkInterface" ).ObjectSet

Global $oTcpipNetworkIntfc = $oRefresher.AddEnum($oWMIService, "Win32_PerfRawData_Tcpip_NetworkInterface" )^ ERROR

_____________________________________________________________________________

Link to comment
Share on other sites

I understood, the reason in WinVista.

Will have to do as in my first post.. >_<

Any ideas for getting received bytes in this OS are welcome.

Edited by Vitas

_____________________________________________________________________________

Link to comment
Share on other sites

I understood, the reason in WinVista.

Will have to do as in my first post.. >_<

Any ideas for getting received bytes in this OS are welcome.

hi,

1st) Try #requireadmin at the very beginning of your script.

2nd) If this don't work, have a look here:

http://msdn.microsoft.com/en-us/library/aa826699(VS.85).aspx

;-))

Stefan

Link to comment
Share on other sites

This code works:

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

Global $str[20], $trimmed = 0, $alltrimmed = 0, $stout = ""
Local $cmd = Run(@comspec & " /c netstat -e", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line

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

While 1
    ;Local $cmd = Run(@comspec & " /c netstat -e", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    Sleep(1000)
    $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, $stout)
                $stout = ""
                $trimmed = 0
                $alltrimmed = 0
            ;MsgBox(0,"", $stout)
        EndIf
    Next

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

This one - doesn't:

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

Global $str[20], $trimmed = 0, $alltrimmed = 0, $stout = ""
;Local $cmd = Run(@comspec & " /c netstat -e", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Local $line

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

While 1
    Local $cmd = Run(@comspec & " /c netstat -e", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
    Sleep(1000)
    $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, $stout)
                $stout = ""
                $trimmed = 0
                $alltrimmed = 0
            ;MsgBox(0,"", $stout)
        EndIf
    Next

    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

I can't understand! Why??

(modified only 8th and 20th lines, I just moved Local $cmd = Run(@comspec & " /c netstat -e", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) in While, for doing it every second.)

Edited by Vitas

_____________________________________________________________________________

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...