Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Good morning, I appreciate any assistance that I can get from the monks out there. I am able to get this to work for me so that I can do a hostname lookup if I only specify one hostname in the script. What I want to do is have a file with hostnames and do lookups for each name in the file. Here is the code that works, and the output:
#!/usr/bin/perl use strict; use warnings; use Net::DNS::Resolver; my $hostname = 'perlmonks.org'; my $res = Net::DNS::Resolver->new( nameservers => [qw(8.8.8.8)], ); my $query = $res->search($hostname); if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "A"; print "Found an A record for $hostname: ".$rr->address; print "\n"; } }
This is the output you get when you run it:
Found an A record for perlmonks.org: 66.39.54.27 Found an A record for perlmonks.org: 216.92.34.251 Found an A record for perlmonks.org: 209.197.123.153
So now taking it a step further, I want to check a whole file of names. This is the code I have, but it's not working. Can someone please help and let me know where I went wrong?
#!/usr/bin/perl use strict; use warnings; use Net::DNS::Resolver; open FILE, "<hostname_check.txt" || die $!; while (<FILE>) { my $res = Net::DNS::Resolver->new( nameservers => [qw(8.8.8.8)], ); my $query = $res->search($_); if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "A"; print "Found an A record for $_: ".$rr->address; print "\n"; } } } close FILE;

In reply to Need help with script with a while loop reading file by brianjb

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 wandering the Monastery: (10)
    As of 2014-04-18 21:21 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (472 votes), past polls