Jump to content
Sign in to follow this  
IvanCodin

DNS Resolution is not working from a list

Recommended Posts

IvanCodin

I can't seem to get the name to resolve to the IP when I use a file a the source.  If I statically assign the dns name it works.  If I read a file and then loop through each line it fails. 

I think I am missing something really simple in my logic but can't seem to figure it out. 

Can someone  point out my logic error?  Would appreciate it.

#include <MsgBoxConstants.au3>
#include <Inet.au3>
#include <File.au3>

$file = @ScriptDir & "\servers.txt"
If FileExists ($file) Then
    $line = FileRead($file)
; TS MsgBox(0,'',$line) ; Show the file content
Else
    MsgBox (0,"Error - File missing", "File was not in " & @ScriptDir)

EndIf

FileOpen($file, 0)
; Servers file is a listing of the server names you wish to lookup.  IE: www.autoitscript.com
; TS MsgBox(0,"TS", "File Open successful")

; Start the TCP service.
TCPStartup()

    ; Assign a Local variable the IP address of www.autoitscript.com.
    Local $sIPAddress = TCPNameToIP("www.autoitscript.com")
    MsgBox(0,"Resolved Name", "autoscript.com resolves to " & $sIPAddress)  ;; <==== This one works!
        For $i = 1 to _FileCountLines($file)
        $find = FileReadLine($file, $i)
        ; TS msgbox(0,'','Line ' & $i & ' is ' & $find)
    Local $sIPAddressTo = TCPNameToIP($find)
        MsgBox(0,"DNS Name", "Name resolves to " & $sIPAddressTo) ;; <===== This one does not work!
        Next
    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "Error code: " & @error)
    Else
        ; Do nothing
    EndIf
TCPShutdown() ; Close the TCP service.

 

Edited by IvanCodin

Share this post


Link to post
Share on other sites
RTFC

Your file I/O is wrong. This works:

#include <MsgBoxConstants.au3>
#include <Inet.au3>
#include <File.au3>

$file = @ScriptDir & "\servers.txt"
If FileExists ($file) Then
    $line = FileRead($file)
Else
    MsgBox (0,"Error - File missing", "File was not in " & @ScriptDir)
EndIf

$filehandle=FileOpen($file, 0)
; Servers file is a listing of the server names you wish to lookup.  IE: www.autoitscript.com

; Start the TCP service.
TCPStartup()

For $i = 1 to _FileCountLines($file)
    $find = FileReadLine($filehandle, $i)
    MsgBox(0,'','Line ' & $i & ' is ' & $find)
    Local $sIPAddressTo = TCPNameToIP($find)
; NB @error needs to be evaluated IMMEDIATELY; any subsequent function all will reset it!<<<<<<
    If @error Then MsgBox($MB_SYSTEMMODAL, "", "Error code: " & @error)
    MsgBox(0,"DNS Name", "Name resolves to " & $sIPAddressTo) ;; <===== This one does not work!
Next
FileClose($filehandle)  ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
TCPShutdown() ; Close the TCP service.

 

Share this post


Link to post
Share on other sites
BrewManNH

Try this, saves some time if reading a large file.

#include <MsgBoxConstants.au3>
#include <Inet.au3>
#include <File.au3>

$file = @ScriptDir & "\servers.txt"
If FileExists($file) Then
    $line = FileRead($file)
Else
    MsgBox(0, "Error - File missing", "File was not in " & @ScriptDir)
EndIf

$filehandle = FileOpen($file, 0)
; Servers file is a listing of the server names you wish to lookup.  IE: www.autoitscript.com

; Start the TCP service.
TCPStartup()
Local $sIPAddressTo ; <<<<<<<<<<<<<<<< don't declare variables in a loop, time wasted
While 1
;~ For $i = 1 To _FileCountLines($file) _FileCountLines is time consuming and not necessary
    $find = FileReadLine($filehandle)
    If @error Then ExitLoop ; <<<<<<<<<<<<<<<<< EOF reached, stop reading the file
    MsgBox(0, '', 'Line ' & $i & ' is ' & $find)
    $sIPAddressTo = TCPNameToIP($find)
    ; NB @error needs to be evaluated IMMEDIATELY; any subsequent function all will reset it!<<<<<<
    If @error Then MsgBox($MB_SYSTEMMODAL, "", "Error code: " & @error)
    MsgBox(0, "DNS Name", "Name resolves to " & $sIPAddressTo) 
;~ Next
WEnd
FileClose($filehandle) ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
TCPShutdown() ; Close the TCP service.

 


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
JLogan3o13

@BrewManNH did you intend to remove that If statement at the top?


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

Share this post


Link to post
Share on other sites
BrewManNH
1 hour ago, JLogan3o13 said:

 did you intend to remove that If statement at the top?

The FileExists line? No, I wasn't focusing on that part, but the check for the file should be in there in some form, otherwise you might try reading a file that isn't there.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
JLogan3o13

Agreed, I just thought you may have missed removing his extra $line and FileRead statement :)

 


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

Share this post


Link to post
Share on other sites
IvanCodin

Thanks for the help and more importantly the explanation!!  I ran a test against a file with  5 names and it works perfectly.

However if I use a large list, I had 100 entries, I get a 11001 error.  My research pointed the post Jos wrote on Nov 4, 2004 that tells me the error is

                     11001 IP_BUF_TOO_SMALL Buffer too small

I can't tell if it's an Autoit issue or an OS issue. How do I increase the buffer or accommodate for a bigger list?  

Share this post


Link to post
Share on other sites
IvanCodin

Did some more research and it looks like the 11001 error is it is a windows displayed when the name cannot be resolved. 

Is there a way to display no name found rather the error? Every thing I try shows I have error in my syntax.

   If @error = 11011 Then
        MsgBox(0,"Name Resolution", "Name " & $find & " was not found")
    Else @error -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Error code: " & @error)
    EndIf

 

Edited by IvanCodin

Share this post


Link to post
Share on other sites
IvanCodin

After a little sleep I went back at re-looked at the suggestions you gave me.  The answer was already in your reply.  It was my lack of understanding that was missing.

It had not dawned on me the if @ error statement you added did exactly what I was looking for. 

As always you guys are awsome!

BrewManNH thanks for the suggestion about using FileReadLine rather than FileCountLines.

 

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  

×