Jump to content

Reverse DNS resolver


Fox2
 Share

Recommended Posts

Func ReverseDNS($IP)

    $t = TimerInit()
    $dnsinfo = "?"
    
    $PingCmd = Run(@ComSpec & " /c ping -a -n 1 " & $IP, "C:\", @SW_HIDE, $STDOUT_CHILD)
    
    While TimerDiff($t) < 2000
        $PingResponseText = StdoutRead($PingCmd)
        If @error Then ExitLoop
        $x1 = StringInStr($PingResponseText, "Pinging")
        $x2 = StringInStr($PingResponseText, "[")
        If $x1 > 0 Then
            If $x2 > 0 Then $dnsinfo = StringStripWS(StringMid($PingResponseText, $x1 + 8, $x2 - $x1 - 8),3)
            ExitLoop
        EndIf
        Sleep(20)
    WEnd

    Return $dnsinfo
    
EndFunc

For anyone who wants it :D

Oddly there doesn't seem to be a copy of this lying round. Feel free to adapt and credit.

Link to comment
Share on other sites

An update in case you're doing a lot of DNS lookup.

This is a quick and dirty way to cache your searches so far and save doing a new command window and PING call for each one.

It caches all confirmed hits in "^" separated chunks, and leaves all misses uncached to retry. (A PING that worked, has worked, but one that didn't may work next time)

Initializing code:

; Initialize DNS lookup cache
Global $DNS_IPCache = "^"
Global $DNS_LookupCache = "^"

Updated Func:

Func ReverseDNS($IP)

  ; Strip WS - needed for caching and for textual use
    $IP = StringStripWS($IP,3)

  ; cached?
    $x = StringInStr($DNS_IPCache,"^" & StringLeft($IP & "                  ",15) & "^")
    if $x > 0 Then
        $entrynum = ($x - 1) / 16 + 1
        $lookupstart = StringInStr($DNS_LookupCache,"^",0,$entrynum) + 1
        $lookupend = StringInStr($DNS_LookupCache,"^",0,$entrynum + 1)
        Return StringMid($DNS_LookupCache, $lookupstart, $lookupend - $lookupstart)
    EndIf

    $t = TimerInit()
    $dnsinfo = "?"

    $PingCmd = Run(@ComSpec & " /c ping -a -n 1 " & $IP, "C:\", @SW_HIDE, $STDOUT_CHILD)
    
    While TimerDiff($t) < 2000
        $PingResponseText = StdoutRead($PingCmd)
        If @error Then ExitLoop
        $x1 = StringInStr($PingResponseText, "Pinging")
        $x2 = StringInStr($PingResponseText, "[")
        If $x1 > 0 Then
            If $x2 > 0 Then $dnsinfo = StringStripWS(StringMid($PingResponseText, $x1 + 8, $x2 - $x1 - 8),3)
          ; and add to cache, padded to allow entry number to be found
            $DNS_IPCache &= StringLeft($IP & "                  ",15) & "^"
            $DNS_LookupCache &= $dnsinfo & "^"
            ExitLoop
        EndIf
        Sleep(20)
    WEnd

    Return $dnsinfo
    
EndFunc

(minor fix posted!)

Edited by Fox2
Link to comment
Share on other sites

Nice! Maybe you could make a GUI or output the results to a file?

The caching method is optimized for speed. Searching an array for a text match is much slower than searching in one string for a text match.

The strings you get look like this:

$DNS_IPCache: ^IP^IP^...^IP^

Each IP is * padded * to 15 characters.

So each (IP+separator) is guaranteed 16 chars.

$DNS_LookupCache: ^DNSINFO^DNSINFO^...^DNSINFO^

There is no information on DNSINFO length, but they are ^ separated ("^" is not a valid character in reverse DNS information so far as I know)

So if you find a match for ^IP^ at position 1, 33, (16n+1), you know the cached text is between the (1st and 2nd), (2nd and 3rd), (N+1) and (n+2) occurance of "^" in the lookup cache. A bit of StringinStr() and you're there. That's how it works.

To make it convert the cache strings to an array, use StringSplit() to split both strings at the "^", ignore the first and last result (null), and remove padding with StringStripWS().

$IPList = StringSplit($DNS_IPCache,"^",1)
$DNSList = StringSplit($DNS_LookupCache,"^",1)
For $entry = 1 to $IPList[0]
    $IPList[$entry] = StringStripWS($IPList[$entry],3)
Next

If $IPList[0] <= 2 then no entries exist, otherwise valid entries start at $IPList[2] and the last entry is one less than $IPList[0]. Macthjing DNS info is in the other table. You can easily rework this to split them into one table with 2 dimensions, or a file.

Edited by Fox2
Link to comment
Share on other sites

Well let me put it this way it occured after I installed my Nvida card

and same thing for my buddy on his desktop but if you take out the card, uninstall the drivers the cmd works again!

And don't tell me 'Thats not possible' like every other person, becuase I'm damn sure it is the problem!

And it sux lol

[center][/center]

Link to comment
Share on other sites

@Dbak

This is Error you get is related to one of the corrupted files :

CAUSE:

This issue may occur if one or more of the following files are missing or damaged:

· Config.nt

· Autoexec.nt

· Command.com

Most probably because of installing software that has corrupted these.

Simple resolution (as i remember for the old NT 4.0 days).

Search your machine for Command.com to begin with and copy it the Winnt\system32 to start with. If it did not solve your problem.

Do the same for Config.NT etc.

Or download this Quickfix 16 Bit SubSystem Quickfix

Enjoy !!

ptrex

Edited by ptrex
Link to comment
Share on other sites

Hi Fox2,

I have been meaning to create a script for reverse DNS and you example script prompted me to have a play.

Your script works well but I decided to create a script using the command 'NSLOOKUP' instead of 'PING'

To me NSLOOKUP is neater than PING as it only sends the reverse DNS commands to the DNS server on the network. In comparison, the PING command sends a reverse DNS lookup to the DNS server and then Pings the IPaddress in question. This is extra traffic on the network which is not actually required.

I also wanted to be able to do a RDNS lookup on a large list of IP addresses so I didn't really want to send these unnecessary pings.

Having said all that, I can still see that using PING is good if you also want to check whether the device is currently active on the network (assuming it responds to pings !)

Anyway Here is my Simple equivalent NSLOOKUP version of the FUNC

CODE
Func _ReverseDNS($IP)

$IP = StringStripWS($IP,3)

$NSLookupCmd = Run(@ComSpec & " /c nslookup "& $IP, "C:\", @SW_HIDE, $STDOUT_CHILD+$STDERR_CHILD)

$ResponseText = StdoutRead($NSLookupCmd)

If @error Then Return

$x1 = StringInStr($ResponseText, "Name:")

$x2 = StringInStr($ResponseText, "Address",0,-1)

If $x1 > 0 and $x2 > 0 Then Return StringStripWS(StringMid($ResponseText, $x1 + 6, $x2 - $x1 - 6),3)

Return "Unknown"

EndFunc

I have also written a GUI to test the FUNC

CODE
#include <GuiIPAddress.au3>

$hGUI = GUICreate("RDNS",600,400)

$idEdit=GUICtrlCreateEdit ("", 0,0,600,350,BitOR($ES_AUTOVSCROLL,$ES_READONLY,$WS_HSCROLL,$WS_VSCROLL))

GUICtrlSendMsg($idEdit, $EM_SetLimitText, 0, 0); Set Edit Text Buffer Size to unlimited

; Create GUI

$idBTNStart=GUICtrlCreateButton ("START", 10,360,90,25)

$idBTNStop=GUICtrlCreateButton ("STOP", 110,360,90,25)

GUICtrlCreateLabel ("START IP", 220,366,50,25)

GUICtrlCreateLabel ("END IP", 420,366,50,25)

$hIPAddressStart = _GUICtrlIpAddress_Create($hGUI, 280, 360)

$hIPAddressEnd = _GUICtrlIpAddress_Create($hGUI, 470, 360)

$IPStart = IniRead("rdns.ini","IpRange","Start","0.0.0.0")

$IPEnd = IniRead("rdns.ini","IpRange","End","0.0.0.0")

_GUICtrlIpAddress_Set($hIPAddressStart, $IPStart)

_GUICtrlIpAddress_Set($hIPAddressEnd, $IPEnd)

GUISetState (@SW_SHOW)

Do

$Msg = GUIGetMsg()

If $Msg = $idBTNStart Then

$IPStart = _GUICtrlIpAddress_Get ($hIPAddressStart)

$IPEnd = _GUICtrlIpAddress_Get ($hIPAddressEnd)

$iIPStart = _IP2Number($IPStart)

$iIPEnd = _IP2Number($IPEnd)

For $iIP = $iIPStart to $iIPEnd

$Msg = GUIGetMsg()

If $Msg = $GUI_EVENT_CLOSE then _Exit()

If $Msg = $idBTNStop then ExitLoop

$sIP = _Number2IP($iIP)

GUICtrlSetData($idEdit,$sIP &" = "& _ReverseDNS($sIP )&@crlf,1)

Next

EndIf

Until $Msg = $GUI_EVENT_CLOSE

_Exit()

Func _ReverseDNS($IP)

$IP = StringStripWS($IP,3)

$NSLookupCmd = Run(@ComSpec & " /c nslookup "& $IP, "C:\", @SW_HIDE, $STDOUT_CHILD+$STDERR_CHILD)

$ResponseText = StdoutRead($NSLookupCmd)

If @error Then Return

$x1 = StringInStr($ResponseText, "Name:")

$x2 = StringInStr($ResponseText, "Address",0,-1)

If $x1 > 0 and $x2 > 0 Then Return StringStripWS(StringMid($ResponseText, $x1 + 6, $x2 - $x1 - 6),3)

Return "Unknown"

EndFunc

Func _Number2IP($IPNumber)

Local $IPString = ""

For $index = 3 to 0 step -1

$X = Int($IPNumber/(256^$index))

$IPNumber = $IPNumber - $X*256^$index

$IPString &= $X &"."

Next

Return StringTrimRight($IPString,1)

EndFunc

Func _IP2Number ($IP)

$IPArray=StringSplit($IP,'.')

$IPValue=($IPArray[1]*(256^3))+($IPArray[2]*(256^2))+($IPArray[3]*(256^1))+($IPArray[4])

Return $IPValue

EndFunc

Func _Exit()

IniWrite("rdns.ini","IpRange","Start",$IPStart)

IniWrite("rdns.ini","IpRange","End",$IPEnd)

EndFunc

My Scripts[topic="73325"]_ReverseDNS()[/topic]Favourite scripts by other members[topic="81687"]SNMP udf[/topic][topic="70759"]Using SNMP - MIB protocol[/topic][topic="39050"]_SplitMon:Section off your monitor!, split your monitor into sections for easy management[/topic][topic="73425"]ZIP.au3 UDF in pure AutoIt[/topic][topic="10534"]WMI ScriptOMatic tool for AutoIt[/topic][topic="51103"]Resources UDF embed/use any data/files into/from AutoIt compiled EXE files[/topic]
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...