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:
Comments and critique are welcome and requested.
Most recent update: 2001-07-12 07:00 CDT
|
#!/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 |
Back to
Code Catacombs