Sign in to follow this  
Followers 0
Rav1

stdoutRead help

9 posts in this topic

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

Share this post


Link to post
Share on other sites



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")

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

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

:)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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
1 person likes this

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

Share this post


Link to post
Share on other sites

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

 

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


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

Share this post


Link to post
Share on other sites

Thanks all! The information and examples are awesome.  You guys help me learn so much and I can't say thanks enough.

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