Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/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

In reply to (code) Resolve list of DNS names by ybiC

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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 2024-04-16 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found