Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Need help with script with a while loop reading file

by brianjb (Novice)
on May 03, 2013 at 13:52 UTC ( #1031893=perlquestion: print w/ replies, xml ) Need Help??
brianjb has asked for the wisdom of the Perl Monks concerning the following question:

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;

Comment on Need help with script with a while loop reading file
Select or Download Code
Re: Need help with script with a while loop reading file
by hdb (Prior) on May 03, 2013 at 13:56 UTC

    Assuming you have one hostname per line in your file you need to add a  chomp; after while (<FILE>) { to remove the newline at the end of the hostname.

      Thank you very much! I indeed forgot the chomp....rookie mistake
Re: Need help with script with a while loop reading file
by blue_cowdawg (Monsignor) on May 03, 2013 at 13:57 UTC

    while (my $hname =<FILE>) { chomp $hname; my $res = Net::DNS::Resolver->new( nameservers => [qw(8.8.8.8)], ); my $query = $res->search($hname); if ($query) { foreach my $rr ($query->answer) { next unless $rr->type eq "A"; print "Found an A record for $_: ".$rr->address; print "\n"; } } }
    I admit I am showing my prejudice here, but I prefer to use implicit variables. That said, note the minor modifications I made to your code, the most important being the "chomp" to remove the EOL from the string. Otherwise you are looking up a hostname that ends in EOL.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
      Thank you for the quick response. Definitely forgot the chomp.
Re: Need help with script with a while loop reading file
by NetWallah (Abbot) on May 03, 2013 at 14:54 UTC
    Move the line
    my $res = Net::DNS::Resolver->new( ...
    outside the "while (<FILE>)" loop - It does not need to be created for each record.

                 "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
            -- Dr. Cox, Scrubs

Re: Need help with script with a while loop reading file
by 2teez (Priest) on May 03, 2013 at 16:23 UTC

    Hi brianjb,
    I know your question has been probably answered :).
    However, if I may draw your attention to some stuff in your code:

    • this
      open FILE, "<hostname_check.txt" || die $!;
      would fail to do what you intended, in case your file doesn't exists.
      It's better written as
      open ( FILE, "<hostname_check.txt" ) || die $!;
      or
      open FILE, "<hostname_check.txt" or die $!;
    • Use lexically scoped filehanles and the 3-argument open function like so:
      open my $fh, '<',"hostname_check.txt" or die $!;
    • Also check the return of close function

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1031893]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-12-25 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (161 votes), past polls