Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (6)
    As of 2014-12-28 20:39 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (182 votes), past polls