Quantcast
Channel: DIY IT Shop » powershell
Viewing all articles
Browse latest Browse all 9

PowerShell: Reverse IP Lookup with DNS

$
0
0

Recently I wrote a script to do a Reverse IP lookup when you don’t have the luxury of Reverse Lookup Zones being configured. If you do happen to have Reverse Lookup Zones configured in your infrastructure, I thought you might like to have this little script that will take advantage of that.

I found a script written by Malek Jakir that did the trick in a simple 4 liner, but in my case I needed it to note the addresses that the lookup failed for or could not ping. I made some modifications to his script that allow that to occur. There is a caveat to this script, however. Since the function called retries the lookup 3 or so times, you’ll see 3 entries for each failed lookup in the output CSV. That is easily handled with Excel and is still much faster to deal with than manually running a ping -a against a list of IPs.

If you know a way to fix the 3 entries for each failure, drop a comment below and I’ll update the script with a credit to you.

As always, be careful and enjoy!

###########################################################################
#
# NAME: DNS.Reverse.Lookup
#
# ORIGINAL BY:  Malek Jakir (http://malekjakir.com/)
# MODIFIED BY: Thomas Rhoads (http://www.diyitshop.com/)
#
# COMMENT: This script can be used to get the DNS reverse entry from the DNS server.
# Put the IP address as one per line in .csv with a header of IP. 
# Result will be generated in CSV file.
#
# VERSION HISTORY:
# 2.0 09/11/2012 - Added Graceful Failure
# 1.0 10/27/2011 - INITAL
#
###########################################################################
Import-CSV “.\IPList.csv” | 
ForEach-Object {

$thisIP = $_.IP

try {
[System.Net.Dns]::GetHostbyAddress($_.IP) | 
Add-Member -Name IP -Value $_.IP -MemberType NoteProperty -PassThru
}
catch {
$myObject = New-Object -Type PSObject
$myObject | Add-Member -Name IP -Value $thisIP -MemberType NoteProperty -PassThru
$myObject | Add-Member -Name HostName -Value "No Ping" -MemberType NoteProperty -PassThru
$myObject
}

} | Select IP, HostName | Export-CSV ".\ReverseLookup.csv" –NoTypeInformation

 


Viewing all articles
Browse latest Browse all 9

Trending Articles