Jump to content

Get Reverse DNS Name


Recommended Posts

Hi all...

I'm having some brain pain trying to figure out how to get the reverse DNS name of a Virtual Machine...

I can get it using Powershell however i cant get the Powershell script to run silently when launched from within an AutoIT script.

This is the Powershell code but i would like to do it from AutoIT.

$VMView_Hostname = gc env:computername

$Ping = Ping $VMView_Hostname | Select-String "Pinging"
$PingSplit = (((($Ping -split'['))[1] -split']'))[0]

$VMView_ip = $PingSplit

$0 = $VMView_ip.Trim().Split(".")[0]
$1 = $VMView_ip.Trim().Split(".")[1]
$2 = $VMView_ip.Trim().Split(".")[2]
$3 = $VMView_ip.Trim().Split(".")[3]

$VMView_ReverseDNS = $3+(".")+$2+(".")+$1+(".")+$0+".in-addr.arpa"

 

 

My main problem is capturing the IP Address of the local PC into a string.

 

Thanks in advance for anyone who takes a look and has any ideas...

Edited by boyemillar
Link to comment
Share on other sites

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,

Edited by sahsanu
Link to comment
Share on other sites

Thanks Sahsanu...

The TCPNameToIP function was exactly what i needed to capture the IP address hadn't seen that before...

I can now split that IP up and reverse it to make my Reverse DNS entry...

#Include <File.au3>
#Include <Toast.au3>
#include <Array.au3>
#include <Inet.au3>

Local $hostname=@ComputerName

TCPStartup()

$tcpnametoip=TCPNameToIP($hostname)
$tcpiptoname=_TCPIpToName($tcpnametoip)


$array = StringSplit ( $tcpnametoip, ".")

$IPAddress = $array[1] & '.' & $array[2] & '.' & $array[3] & '.' & $array[4]

$ReverseDNSName = $array[4] & '.' & $array[3] & '.' & $array[2] & '.' & $array[1] & '.in-addr.arpa'
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...