Sign in to follow this  
Followers 0
AndyS01

Stop sending MSDOS output to AutoIT output window

11 posts in this topic

I have a function that uses Run() to run a "nslookup" command and check to see if the address passed to it is reachable.

After invoking the Run() command, I use calls to StderrRead() to read nslookup's output. I'm able to see the output, but it also displays in AutoIT's console output window (F8).

I would like to keep nslookup's output from showing up in AutoIT's output window because I use ConsoleWrite() to output progress messages.

I tried redirecting nslookup's output to a file, but nslookup complained.

Here is some test code:

_NS_Lookup("www.google.com")
Func _NS_Lookup($ip)
Local $ret, $str
$ret = Run("nslookup " & $ip , @SystemDir, @SW_HIDE, 4)
$str = ""
While 1
$str &= StderrRead($ret)
If @error Then ExitLoop
WEnd
If (StringInStr($str, "can't find " & $ip)) Then
$ret = False
Else
$ret = True
EndIf
Return ($ret)
EndFunc ;==>_NS_Lookup

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Try this:

#include <Constants.au3>

$NSLookupURL = InputBox("NSLookup", "Enter the URL to perform the Name Server Lookup on.", "www.bing.com")
$Result = _NS_Lookup($NSLookupURL)

If $Result = True Then
    MsgBox(0, "Debug", $NSLookupURL & " is reachable")
Else
    MsgBox(0, "Debug", $NSLookupURL & " isn't reachable")
EndIf

Func _NS_Lookup($ip)
    Local $OutLine

    Local $nslookup = Run(@ComSpec & " /c nslookup " & $NSLookupURL, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)
    While 1
        $line = StdoutRead($nslookup)
        If @error Then ExitLoop
        $OutLine &= $line
    WEnd

    ;MsgBox(0, "Debug", $OutLine)

    If (StringInStr($OutLine, "can't find " & $ip)) Then
        $ret = False
    Else
        $ret = True
    EndIf
    Return ($ret)
EndFunc   ;==>_NS_Lookup

HI!

Edited by Nessie

My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Run(@ComSpec &amp; "nslookup " &amp; $ip , @SystemDir, @SW_HIDE, 4)

@comspec macro is not required to run console based applications, this method is abused so often in this forum it is tragic, why because it saves you having to differentiate between cmd.exe native commands and those that are executable files?

If you didn't use it then your code line would not have the bug it currently has.

Edited by Mobius

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

If I'm honest, I've never used it until I just tested it then and it gave desired result.

If you had copy and pasted then run what you posted the only result would have been an invalid exe path error.

The point is that yes the method works (else it would not be considered a method to use for so bleeding long) but running two programs where one is sufficient isn't clever or recommendable.

Not ragging on just you dude, not really your fault, apart from your use of it actually caused you to post non working code which was otherwise a sound suggestion.

Anyway rant over going back into cold storage.

Vlad

Edited by Mobius

Share this post


Link to post
Share on other sites

I used the code that Nessie provided and it worked like I wanted it to (thank you very much).

How would you change that code to run without @comspec?

Share this post


Link to post
Share on other sites

Actually, I changed the Run line to look like this and it worked OK:

$ret = Run("nslookup " & $ip, @SystemDir, @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)

Share this post


Link to post
Share on other sites

The solution was to pass the "$STDERR_MERGED" flag and to read from StdOut.

Share this post


Link to post
Share on other sites

Glad to help you;)


My UDF: NetInfo UDF Play with your network, check your download/upload speed and much more! YTAPI Easy to use YouTube API, now you can easy retrive all needed info from a video. NavInfo Check if a specific browser is installed and retrive other usefull information. YWeather Easy to use Yahoo Weather API, now you can easily retrive details about the weather in a specific region. No-IP UDF Easily update your no-ip hostname(s).

My Script: Wallpaper Changer Change you wallpaper dinamically, you can also download your wallpaper from your website and share it with all!   My Snippet: _ImageSaveToBMPConvert an image to bmp format. _SciteGOTO Open a file in SciTE at specific fileline. _FileToHex Show the hex code of a specified file

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