#!/usr/bin/perl -w # # Run through a list of ip addresses and "stealthly" get # the fqdn's for them. # Hopefully, this will be a "quiet" zone transfer tool. :-) # Harry Hoffman # use strict; use Net::DNS; use Net::IP; use Getopt::Long; use diagnostics; use vars qw($opt_h $opt_n $opt_o $opt_s @servers $n $res $packet $iaddr $record $fqdn); $| = 1; GetOptions('help|usage' => \$opt_h, "netblock=s" => \$opt_n, "outfile|write=s" => \$opt_o, "servers=s" => \$opt_s ); sub usage { my $name = $0; print << "EOF"; Usage: $name -h Usage: $name -n -w Usage: $name -n -w -s Usage: This script requires the "-n" flag followed by an ipaddress/cidr including the slash between them. The "-w" flag takes an argument of the directory and file to write the mappings of ipaddress to fqdn. It is required. The "-s" flag is optional and should be a file containing a list of nameservers, one per line. EOF exit(0); } sub getnsservers { open (SERVERS, "$opt_s") or die "Can't open $opt_s, $!\n"; while() { chomp; next if /^$/; if (/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) { push (@servers, $_); } } close (SERVERS); } { my $count=0; sub lookup { my @nsservers = @servers; $n = int($count++ % $#nsservers); $res = Net::DNS::Resolver->new; $res->nameservers("$nsservers[$n]"); $packet = $res->query("$iaddr","PTR"); if ($packet) { $record = ($packet->answer)[0]; $fqdn = $record->rdatastr; chop($fqdn); } } } if ($opt_s) { getnsservers(); } else { @servers = ( "144.118.24.20", "144.118.24.10", "155.247.166.2", "155.247.19.2", "128.175.13.17", "128.91.2.13", "128.91.254.1", "18.72.0.3", "128.186.6.103", "128.186.8.8", "146.186.163.66", "130.132.1.9", "130.132.1.10", "130.132.1.11", "130.132.89.9", "128.135.4.2", "128.135.12.73", "128.135.20.100", "128.135.228.2", "128.109.131.40", "152.3.250.1", "152.3.250.2" ); } if ($opt_h || !$opt_o || !$opt_n) { usage(); } my $ip = new Net::IP("$opt_n") or die "No netblock/cidr supplied.\n"; open(OUTFILE, "> $opt_o") or die "Can't open file: $! \n"; do { $iaddr = $ip->ip(); lookup($iaddr); if($fqdn) { print OUTFILE $iaddr."\t". $fqdn."\n"; } else { print OUTFILE $iaddr."\t"."unassgn"."\n"; } $fqdn = ""; } while (++$ip); close(OUTFILE) or die "$opt_o didn't close $!";