Jump to content

stdoutRead help


Go to solution Solved by MHz,

Recommended Posts

Hey gang.  I have a script that I am working with that I pieced together (I'm learning) that I was hoping someone can help me with.  I am trying to read the ipconfig /all cmd window and get the dhcp server address.  The script works 80% of the time but occasionally the script returns the line below the dhcp server address.  

for example:

1.1.1.1

DNS Servers . . . . . . . . . . .>Exit code: 0    Time: 1.296
 
Thoughts?
 
Thanks,
Rav
#include <MsgBoxConstants.au3>
Local $sPrinter = InputBox("Add Printer", "Enter the complete Iprint name of the printer to add.", "Enter Printer Name", "", _
             - 1, -1, 0, 0)
ipconfig()
Func ipconfig($lookfor = "DHCP Server")
    $cmd = Run(@Comspec & " /c ipconfig /all", "" , @SW_HIDE, 2+4)
    $Result = ""
    While 1
        $line = StdoutRead($cmd)
        If @error Then Return "Error"
        if StringInStr($line,$lookfor) then ExitLoop
    WEnd
    $Data = StringSplit(StringStripWS($line,7),$lookfor,1)
    $Data = StringSplit($Data[2],":",1)
    $Data = StringStripWS($Data[2],7)
    ;FileWrite("Y:\files\printer_install.bat", @CRLF & "iprntcmd.exe -a ipp://"&$Data&"/ipp/"&$sPrinter)
    ConsoleWrite($Data)
    Return $Data

EndFunc
Link to comment
Share on other sites

  • Moderators

Why not just write it out to a temp file and then parse that?

Pseudo, on my phone...

Run(@Comspec & " /c ipconfig /all >" & @TempDir & "\MyIP.txt")

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

#include <AutoItConstants.au3>
#include <String.au3>
$ipid = Run('"' & @ComSpec & '" /c ipconfig /all | find "DHCP-"', '', @SW_HIDE, $STDOUT_CHILD)
ProcessWaitClose($iPID)
$aDHCP = _StringBetween(StdoutRead($iPID),": ",@LF)
MsgBox(262144, Default, $aDHCP[0], 0)

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

  • Solution

Nice reply Exit. I will attempt to do a little more with cmd by using a for loop.

$result = ipconfig()
ConsoleWrite($result & @CRLF)

Func ipconfig($lookfor = "DHCP Server")
    ; run cmd hidden with stdout
    Local $cmd = Run(@Comspec & ' /c @echo off & ' & _
     'for /f "usebackq tokens=2 delims=:" %# in (`ipconfig /all ^| find "' & $lookfor & '"`) do echo %#', "" , @SW_HIDE, 2 _
    )
    ; wait for the process to close
    ProcessWaitClose($cmd)
    ; return the result with each end trimmed of whitespace
    Return StringStripWS(StdoutRead($cmd), 3)
EndFunc

:)

Link to comment
Share on other sites

You can also use WMI for that, with >network.au3

#Include "Network.au3"
$aInfos = _GetNetworkAdapterInfos()
For $i = 0 To UBound($aInfos) - 1
    If StringInStr($aInfos[$i][20], ".") Then ConsoleWrite( "[" & $aInfos[$i][8] & "] DHCP Server : " & $aInfos[$i][20] &  @CRLF)
Next
Link to comment
Share on other sites

are you just experimenting with stdoutread or are you interested in network parameters?

if you are intereste to network parameters then >also this link may be of interest:

with that udf you can retrieve some network parameters in this way for example:
(see comments inside the udf for details on returned values)
 

#include <array.au3>
#include "_NetworkStatistics.au3" ; get this from the following link:
; http://www.autoitscript.com/forum/topic/151920-network-interface-info-statistics-and-traffic/?p=1088781

    Local $_net = _Network_IPAllAddressTable()
    _ArrayDisplay($_net)

edit:

added link

Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Chimp, thanks for the link ! It will be very usefull

 

You are welcome...... but we have to thanks Ascend4ant :)

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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...