Hello,
Just a quick and dirty example using nslookup command:
#include <Constants.au3>
Local $hostname="autoitscript.com" ;just a hostname example using autoitscript.com
Local $DNSserver="8.8.8.8" ;just an example using Google DNS server (if you want to use default server just remove the ip: Local $DNSserver=""
$GETNORMAL=_NormalLookup($hostname,$DNSserver)
If @error Then
MsgBox(0,"","Error trying to get ip for " & $hostname)
Exit
Else
MsgBox(0,"","Normal Lookup for hostname " & $hostname & " is " & $GETNORMAL)
EndIf
$GETREVERSE=_ReverseLookup($GETNORMAL,$DNSserver)
If @error Then
MsgBox(0,"","Error trying to get reverse lookup for " & $hostname)
Exit
Else
MsgBox(0,"","Reverse Lookup for ip " & $GETNORMAL & " is " & $GETREVERSE)
EndIf
Func _ReverseLookup($fip="127.0.0.1",$fDNSserver="")
Local $Consigue=Run(@ComSpec & " /c " & 'nslookup -type=PTR ' & $fip & ' ' & $fDNSserver, "", @SW_HIDE,$STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
$line &= StdoutRead($Consigue)
If @error Then ExitLoop
WEnd
Local $array=StringRegExp($line,".*name\ =\ (.+)?",3)
If @error Then
SetError(1)
Else
Return StringStripWS(StringStripCR($array[0]),8)
EndIf
EndFunc
Func _NormalLookup($fhostname="localhost",$fDNSserver="")
Local $Consigue=Run(@ComSpec & " /c " & 'nslookup -type=A ' & $fhostname & ' ' & $fDNSserver, "", @SW_HIDE,$STDERR_CHILD + $STDOUT_CHILD)
Local $line
While 1
$line &= StdoutRead($Consigue)
If @error Then ExitLoop
WEnd
Local $array=StringRegExp($line,"(?s)Name:.*Address:\ (.+)",3)
If @error Then
SetError(1)
Else
Return StringStripWS(StringStripCR($array[0]),8)
EndIf
EndFunc
Keep in mind what I said, it is quick and really dirty, it will fail if the hostname has more than one ip, etc. so I recommend to take a look to already implemented functions TCPNameToIP and _TCPIpToName (check the examples in AutoIT help).
Edit: To add example using TCP functions:
#include <Inet.au3>
Local $hostname=@ComputerName
TCPStartup()
$tcpnametoip=TCPNameToIP($hostname)
$tcpiptoname=_TCPIpToName($tcpnametoip)
MsgBox(64,"Reverse Lookup", "Hostname " & $hostname & " resolves to " & $tcpnametoip & @CRLF & _
"IP " & $tcpnametoip & " reverses to " & $tcpiptoname)
Cheers,