powershellbatch-filevbscriptdnsnslookup

Compare "nslookup host" to "nslookup ip"


I am in need to determine if DNS lookup by host matches the output of DNS lookup by IP on the Windows platform using standard windows tools (CMD, VBS, POWERSHELL).

Here is an example -

  1. Query the hostname (haw1) using NSLOOKUP

    C:>nslookup haw1
    Server:  myserver.blah.org
    Address:  172.22.14.10
    
    Name:    haw1.blah.org
    Address:  172.40.82.70
    
  2. Then query the IP address from the result in #1

    C:>nslookup 172.40.82.70
    Server:  myserver.blah.org
    Address:  172.22.14.10
    
    Name:    ghi1.blah.org
    Address:  172.40.82.70
    
  3. Compare the results to see if they match. In this example haw1.blah.org <> ghi.blah.org

  4. Output something like "haw1.blah.org does not match" or "haw1.blah.org matches" depending on the results.

Any idea how to accomplish this?

Adding what I have so far...

This leaves me with the FQDN of the item and the IP the name returns from DNS. I am at a loss how to then proceed to do the reverse DNS lookup and the comparison.

What I have is only a partial solution.

if exist name.txt del name.txt
if exist address.txt del address.txt
nslookup %1 > out.txt
type out.txt | find /v /i "server" >out1.txt
del out.txt
type out1.txt | find /v /i "172.22.14.10" >out.txt
del out1.txt
type out.txt | find /i "name" > name.txt
type out.txt | find /i "address:" > address.txt
del out.txt
for /f "tokens=2" %%i in (name.txt) do echo %%i >name1.txt
del name.txt
for /f "tokens=2" %%i in (address.txt) do echo %%i >address1.txt
del address.txt

Solution

  • As TessellingHeckler pointed out, there are many cases where this approach will fail. With that said here is a PowerShell method using the Net.DNS from .Net that should be usable on Windows 7:

    $ComputerName = "haw1"
    $NameLookup = [Net.DNS]::GetHostEntry($ComputerName)
    $IPAddress = @($NameLookup.AddressList)[0].IPAddressToString
    $IPLookup = [Net.DNS]::GetHostEntry($IPAddress)
    $NameLookup.HostName -eq $IPLookup.HostName