0

I'm trying to write a script that will take input from a .txt file of IP address. Do a reverse DNS lookup to get the hostname of the IP address and then export that data into .csv file.

Here is what I have so far.

# getting the IP's from the file


$IPADDR = Get-Content "C:\Users\douglasfrancis\Desktop\IP_Test.txt"


ForEach ($IPADDR in $IPADDR)
{
  [System.Net.DNS]::GetHostbyAddress($IPADDR) | Add-Member -Name IP -Value $IPADDR -    MemberType NoteProperty -PassThru | select IP, HostName | sort -property Hostname | export-    csv "C:\Users\douglasfrancis\Desktop\ReverseLookup.csv" 

}

How it is now the created CSV file will have the column heads that I assigned and the last IP address in the list with its hostname. So somehow its dropping everything else.

If I run it with the export-csv commented out then all the IP's are displayed in the console but are not sorted by hostname.

I've used this same basic pipe before with no issues so I'm kinda at a loss for what is going on here. Any help would be awesome.

Thanks,

2 Answers2

1

Took break and came back to it. Realized what I was missing and this now works as it should.

$SortIP =@()
ForEach ($IPADDR in $IPADDR)
{
  $SortIP += [System.Net.DNS]::GetHostbyAddress($IPADDR) | Add-Member -Name IP -Value     $IPADDR -MemberType NoteProperty -PassThru | select IP, HostName 
}

$SortIP | sort -property Hostname | export-csv "C:\Users\douglasfrancis\Desktop\ReverseLookup.csv" -NoTypeInformation

basically added the "$SortIP" var in and used "+= [System.Net.DNS]" instead of the "=[System.Net.DNS]" that I was using earlier and it does exactly what I expected it to.

0

This has potential for me to be a big help. I have been trying to add to the script so that it will recorded the entries that do no have a DNS Reverse Lookup. In the file I am running I have 9 IPs in a list of 2700 that do not have a reverse.

So far I have only been able to record the successful. The 9 files that I am trying to also include get an error like this:

ERROR: Exception calling "GetHostByAddress" with "1" argument(s): "The requested name is valid, but no data of the requested type was found"
ERROR: At C:\SANDBOX\Reverse DNS Pipe fails CSV with SORT.ps1:21 char:48
ERROR: +   $SortIP += [System.Net.DNS]::GetHostbyAddress <<<< ($IPADDR) | Add-Member -Name IP -Value $IPADDR -MemberType NoteProperty -PassThru | select IP, HostName
ERROR:     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
ERROR:     + FullyQualifiedErrorId : DotNetMethodException
ERROR:

This is the script as I am running it:

# getting the IP's from the file

$IPADDR = Get-Content "C:\SANDBOX\inputips.txt"
$SortIP =@()

foreach ($IPADDR in $IPADDR)
{
  $SortIP += [System.Net.DNS]::GetHostbyAddress($IPADDR) | Add-Member -Name IP -Value $IPADDR -MemberType NoteProperty -PassThru | select IP, HostName 
}

$SortIP | sort -property Hostname | export-csv "C:\SANDBOX\ReverseLookupSORT.csv" -NoTypeInformation
kasperd
  • 29,894
  • 16
  • 72
  • 122
  • @kasperd this is what I've got working. – Douglas Francis Sep 05 '14 at 20:02
  • `code $IPADDR = Get-Content "C:\IP_test.txt" $IPADDR = $IPADDR | select -uniq #This does the Reverse DNS lookup and exports success' and failures to files. $(ForEach ($IPADDR in $IPADDR) { Try { [System.Net.DNS]::GetHostbyAddress($IPADDR) | Select Hostname,@{label='IP';expression={$IPAddr}} } Catch { Add-Content -Value "$IPADDR failed lookup" -Path "C:\ReverseLookup_failed.csv" } } ) | Sort -Property Hostname | Export-Csv "C:\ReverseLookup.csv" -NoTypeInformation ` – Douglas Francis Sep 05 '14 at 20:02