<?xml version="1.0" encoding="windows-1252"?>
<node id="95821" title="(code) Resolve list of DNS names" created="2001-07-11 19:24:45" updated="2005-08-15 09:40:52">
<type id="1748">
sourcecode</type>
<author id="14909">
ybiC</author>
<data>
<field name="doctext">
&lt;code&gt;#!/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   =&gt; 'resolve.in',
   csv  =&gt; 'resolve.csv',
   xls  =&gt; '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} &amp;&amp; -T _) {
   print ("Error reading input file \"$file{in}\": $!\n\n");
   &amp;USAGE();
   }
@ARGV = $file{in};
print ("  Reading input file $file{in}\n");
chomp (@ARGV = &lt;&gt;);


# 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) {
   &amp;UNLINK($_, 'verbose');
   }


# get down to business
print "  Resolving names...\n";
open (CSVFILE, "&gt;&gt;$file{csv}") or die "Error opening $file{csv} for append: $!";
print CSVFILE "hostname,IPaddr\n";
foreach my $called (@ARGV) {
   my $res = new Net::DNS::Resolver;
   $res-&gt;nameservers($nameserver);
   my $query = $res-&gt;search("$called");
   if ($query) {
      my $rr;
      foreach $rr ($query-&gt;answer) {
         next unless $rr-&gt;type eq "A";
         print CSVFILE "$called,", $rr-&gt;address, "\n"
	    or die "Error printing to $file{csv}: $!";
         }
      } else {
      print CSVFILE "$called,query failed: ", $res-&gt;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 -&gt; new($file{xls});
my $worksheet = $workbook -&gt; addworksheet();
my $row      = 0;
while (&lt;CSVFILE&gt;) {
   chomp;
   my @field = split(',', $_);
   my $column = 0;
   foreach my $token (@field) {
      $worksheet -&gt; write($row, $column, $token);
      $column++;
      }
   $row++;
   }
my $format1 = $workbook -&gt; addformat();
   $format1 -&gt; set_bold();
   $format1 -&gt; set_align('center');
   $format1 -&gt; set_bg_color('tan');
   #$format1 -&gt; set_border();
$worksheet -&gt; set_row(0, undef, $format1);
$workbook  -&gt; 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 &amp;&amp; -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 &lt;&lt;EOF

Usage : $0 &lt;enter&gt;
      where $file{in} is textfile listing device DNS name, one per line.

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 &lt;enter&gt;

=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



&lt;/code&gt;</field>
<field name="codedescription">
Feed this script a textfile list of hostnames, and it spits out CSV and XLS files of names and resolved IP addresses. &amp;nbsp; I use this ditty to confirm &lt;i&gt;(or deny)&lt;/i&gt; in one swell foop that the DNS folks have proper IP address records for every one of the hundred+ LAN switches I support.

&lt;p&gt;
Tested with:
&lt;ul&gt;
  &lt;li&gt;Net::DNS 0.12
  &lt;li&gt;Spreadsheet::WriteExcel 0.31
  &lt;li&gt;Perl 5.00503
  &lt;li&gt;Debian 2.2r3
&lt;/ul&gt;

&lt;p&gt;
Comments and critique are welcome and requested.

&lt;p&gt;
&lt;b&gt;Most recent update: &lt;/b&gt; 2001-07-12 07:00 CDT
&lt;br&gt;Corrected minor mistakes in comments and above description.
&lt;br&gt;Add a bit more explanation to description above.</field>
<field name="codecategory">
Networking Code</field>
<field name="codeauthor">
[ybiC]</field>
</data>
</node>
