Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

(code) Resolve list of DNS names

by ybiC (Prior)
on Jul 11, 2001 at 23:24 UTC ( #95821=sourcecode: print w/replies, xml ) Need Help??
Category: Networking Code
Author/Contact Info ybiC
Description: Feed this script a textfile list of hostnames, and it spits out CSV and XLS files of names and resolved IP addresses.   I use this ditty to confirm (or deny) in one swell foop that the DNS folks have proper IP address records for every one of the hundred+ LAN switches I support.

Tested with:

  • Net::DNS 0.12
  • Spreadsheet::WriteExcel 0.31
  • Perl 5.00503
  • Debian 2.2r3

Comments and critique are welcome and requested.

Most recent update: 2001-07-12 07:00 CDT
Corrected minor mistakes in comments and above description.
Add a bit more explanation to description above.

#!/usr/bin/perl -w

# resolve.pl
# pod at tail

use strict;
use Net::DNS;
use Spreadsheet::WriteExcel;

my $nameserver = '172.30.4.73';
my %file = (
   in   => 'resolve.in',
   csv  => 'resolve.csv',
   xls  => 'resolve.xls',
   );
my @unlink = (
   $file{csv},
   $file{xls},
   );


# files writable only by this user
umask oct 133;
print "\nStarting $0\n";


# read list of hostnames from input file
unless (-r $file{in} && -T _) {
   print ("Error reading input file \"$file{in}\": $!\n\n");
   &USAGE();
   }
@ARGV = $file{in};
print ("  Reading input file $file{in}\n");
chomp (@ARGV = <>);


# sanity-check input file
# "exit" not "die" so no Perl error to console
print "  Validating target list...\n";
foreach my $nonsane (@ARGV) {
   chomp $nonsane;
   unless ($nonsane =~ (/^(\w|-|\.)+$/)) {
      print(
         "\n  Ack - \"$nonsane\" is improperly formatted.\n",
         "  Only alphanumeric, underscore, dash and dot allowed.\n\n",
         "  Edit $file{in} or re-enter targets to remove puctuation,",
         "blank lines and/or blank spaces.\n\n",
         );
      exit;
      }
   }
   print ("  Specified targets names all valid.\n");


# delete pre-existing out file
for(@unlink) {
   &UNLINK($_, 'verbose');
   }


# get down to business
print "  Resolving names...\n";
open (CSVFILE, ">>$file{csv}") or die "Error opening $file{csv} for ap
+pend: $!";
print CSVFILE "hostname,IPaddr\n";
foreach my $called (@ARGV) {
   my $res = new Net::DNS::Resolver;
   $res->nameservers($nameserver);
   my $query = $res->search("$called");
   if ($query) {
      my $rr;
      foreach $rr ($query->answer) {
         next unless $rr->type eq "A";
         print CSVFILE "$called,", $rr->address, "\n"
        or die "Error printing to $file{csv}: $!";
         }
      } else {
      print CSVFILE "$called,query failed: ", $res->errorstring, "\n"
         or die "Error printing to $file{csv}: $!";
      }
   }
close (CSVFILE) or die "Error closing $file{csv}: $!";


# create Excel binary format outfile from CSV
print ('  Munging csv outfile to xls binary format - ');
open (CSVFILE, $file{csv}) or die "Error opening $file{csv}: $!";
my $workbook  = Spreadsheet::WriteExcel -> new($file{xls});
my $worksheet = $workbook -> addworksheet();
my $row      = 0;
while (<CSVFILE>) {
   chomp;
   my @field = split(',', $_);
   my $column = 0;
   foreach my $token (@field) {
      $worksheet -> write($row, $column, $token);
      $column++;
      }
   $row++;
   }
my $format1 = $workbook -> addformat();
   $format1 -> set_bold();
   $format1 -> set_align('center');
   $format1 -> set_bg_color('tan');
   #$format1 -> set_border();
$worksheet -> set_row(0, undef, $format1);
$workbook  -> close() or die "Error closing $workbook: $!";
print ("done.\n");


# Wrap it all up
print ("Completed run of $0.\nResults at $file{csv} and $file{xls}\n\n
+");
exit;


######################################################################
+#####
# cleanup files when done with them
sub UNLINK {
   my $file = $_[0];
   my $echo = $_[1];
   if (-e $file && -w _) {
      print "  Unlinking $file..."        if ($echo eq 'verbose');
      unlink $file or die "Error unlinking $file: $!";
      print "  *poof*\n"               if ($echo eq 'verbose');
      }
   }
######################################################################
+#####
# don't really need to es'plain this one
sub USAGE {
print <<EOF

Usage : $0 <enter>
      where $file{in} is textfile listing device DNS name, one per lin
+e.

perldoc $0 for a few more details.

    This script              $0
    Net::DNS                 $Net::DNS::VERSION
    Spreadsheet::WriteExcel  $Spreadsheet::WriteExcel::VERSION
    Perl                     $]
    Local OS                 $^O

EOF
   ;
   exit;
   }
######################################################################
+#####


=head1 Name

resolve.pl

=head1 Summary

Feed this script a textfile list of DNS names, and it spits back
CSV and XLS files of names and IPaddrs.

 Comments and critique are always welcomed.

=head1 Usage

 resolve.pl <enter>

=head1 Tested

 Net::DNS                 0.12
 Spreadsheet::WriteExcel  0.31
 Perl                     5.00503
 Debian                   2.2r3

=head1 Updated

 2001-07-11   17:25 CDT
   Correct minor mistakes in comments.
   Post to PerlMonks.
   Initial working code.

=head1 ToDos

 Accept IPaddrs from infile and resolve to name.

=head1 Author

 ybiC

=cut
Replies are listed 'Best First'.
Re: Resolve list of DNS names
by mikeB (Friar) on Jul 11, 2001 at 23:53 UTC
    Check out this recent thread on validating IP addresses. It's not quite as simple as you might think.
      Thanks for the thread-link, mikeB.   8^)

      However, my infile sanity-check isn't intended to parse or untaint IP addresses.   If I ever do add support for IP address input (reverse-lookups), I'll likely look to CPAN for a module.   A quick search turned up these candidates: Net::IPAddr-Find, Net::IPv4Addr, Net::IP, and NetAddr::IP.
          cheers,
          Don
          striving toward Perl Adept
          (it's pronounced "why-bick")

        Get the following error. What should i do? print (...) interpreted as function at ./resolve.pl line 112. syntax error at ./resolve.pl line 120, near "sub UNLINK " Can't use global @_ in "my" at ./resolve.pl line 121, near "= $_" syntax error at ./resolve.pl line 128, near "}" Execution of ./resolve.pl aborted due to compilation errors.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://95821]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2023-12-11 02:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?