/* ]]> */
Feb 012011
 

I recently had the need to reconcile IP Addresses against HostNames to ensure that my local DNS server had an accurate match. Since I needed to do this quickly, I used invoke-expression to call NsLookup.exe but you can undoubtedly achieve the same result directly with .Net methods.

To do this in a script, we need to do some parsing of NsLookup’s multiline output. Also, for my situation, the original hostname is not fully-qualified, so (as you can see below) I strip off any domain name suffix from the reverse lookup results.

Here’s the code:

?View Code POWERSHELL
$IpAddress = "192.168.1.1"
$Hostname = "myHostname"
 
$revLookupName = iex "nslookup $IpAddress" |? {$_ -like "name*"} |% {$_.split(":")[1].trim()}
 
write-host "NameMatch for $IpAddress  : " -nonewline -fore darkcyan
write-host "$($n.split(".")[0].tolower()) = " -nonewline
write-host "$($Hostname.tolower()) ? " -nonewline
 
if  ($revLookupName.split(".")[0].tolower() -eq ($Hostname.tolower()) {
        write-host "OK" -fore green
        }
else {
        write-host "NOMATCH" -fore red
        }

 Posted by at 12:35 pm

  One Response to “DNS Reverse Lookup”

  1. useful piece of code, right what I needed. thanks.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Page optimized by WP Minify WordPress Plugin