Need help with perl script with a while loop reading file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with perl script with a while loop reading file
# 1  
Old 05-03-2013
Need help with perl script with a while loop reading file

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:

Code:
#!/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:

Code:
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?

Code:
#!/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;

---------- Post updated at 08:25 AM ---------- Previous update was at 08:01 AM ----------

I found the mistake.

Here is the updated code that works:

Code:
#!/usr/bin/perl
use strict;
use warnings;

use Net::DNS::Resolver;

my $res = Net::DNS::Resolver->new(
  nameservers => [qw(8.8.8.8)],
);

open FILE, "<hostname_check.txt" || die $!;
while (<FILE>) {
chomp;
my $hostname = $_;
my $query = $res->search($hostname);

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;

Here is the sample output:

Quote:

Found an A record for google.com: 74.125.226.192
Found an A record for google.com: 74.125.226.193
Found an A record for google.com: 74.125.226.201
Found an A record for google.com: 74.125.226.199
Found an A record for google.com: 74.125.226.197
Found an A record for google.com: 74.125.226.194
Found an A record for google.com: 74.125.226.195
Found an A record for google.com: 74.125.226.206
Found an A record for google.com: 74.125.226.196
Found an A record for google.com: 74.125.226.198
Found an A record for google.com: 74.125.226.200
Found an A record for perl.org: 207.171.7.53
Found an A record for perl.org: 207.171.7.43
Found an A record for yahoo.com: 98.139.183.24
Found an A record for yahoo.com: 98.138.253.109
Found an A record for yahoo.com: 206.190.36.45
# 2  
Old 05-04-2013
Sightly off topic,

Quote:
open FILE, "<hostname_check.txt" || die $!;
Even you you provide a non existence file, die would not be executed and you might screw up your time to troubleshoot the problem. The reason is "||" will return true or every non-empty string you provide as filename.

Always use or instead of || to avoid this.

If you do want to use ||, supply the parenthesis to open call.

Code:
open (FILE, "<hostname_check.txt") || die $!;

or
Code:
open FILE, "<hostname_check.txt" or die $!;


In general, using parenthesis is always a good practice.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sequential Reading from two file in a loop

Hello All, I have two files with me file1.txt and file2.txt file1.txt has: 333 222 111 file2.txt has ccc bbb aaa ccc is related to 333 only, bbb is related to 222 only and aaa is related to 111 only. I have to get the values from each of the file and pass them in the URL... (3 Replies)
Discussion started by: ankur328
3 Replies

2. Shell Programming and Scripting

Removing \r and \n during reading file through while loop

Hi, I am writing in a file through cat command. This file will contain the path of file along with filename. e.g. /home/user/folder1/folder2/filename.txt There might be very large number of this path in same file like say 140 when I try to run while command: while read -r file do //command... (8 Replies)
Discussion started by: Pulkit Lall
8 Replies

3. Shell Programming and Scripting

Error while reading variable from a file in perl script

I have a file abc.ini and declared many variables in that file, one of the variable(DBname) value I am trying to read in my perl script but getting error. File abc.ini content # database name DBname =UATBOX my $ex_stat; my $cmd_output; $ex_stat = "\Qawk '/^DBname/{print... (2 Replies)
Discussion started by: Devesh5683
2 Replies

4. Shell Programming and Scripting

Perl Script for reading table format data from file.

Hi, i need a perl script which reads the file, content is given below. and output in new file. TARGET DRIVE IO1 IO2 IO3 IO4 IO5 ------------ --------- --------- --------- --------- --------- 0a.1.8 266 236 ... (3 Replies)
Discussion started by: asak
3 Replies

5. Shell Programming and Scripting

Help need in perl script reading from file

Need perl script, data file will be csv format. I have text file contains 2 colums. Filename Foldernumber aaaa 13455 bbbb 23465 cccc 26689 I have two location 1. files present and 2. folders present. I need to search for file and folder if folder... (3 Replies)
Discussion started by: hnkumar
3 Replies

6. Shell Programming and Scripting

SOLVED: reading config file in a perl script

Hi! I have a need to do this in Perl. script.pl -config file The script would be doing a wget/LWP on a URL which is defined in the config file. So when I run the script it should return either one of these conditions - 1) OK with exit status 0. Should also print "wget URL" 2)... (6 Replies)
Discussion started by: jacki
6 Replies

7. Shell Programming and Scripting

Help with perl script while reading a file

Hi Everyone, I am very new to perl, but came across a situation wherein I have to read a c++ header file and write the datatype, its identifier and also the length to an excel file. There can be other header files, in the directory but I should browse through the file which has only "_mef:" string... (9 Replies)
Discussion started by: ramakanth_burra
9 Replies

8. AIX

How to pause a while loop while reading from a file

Hi, I am building a script to grep for a string in all the files from a folder and display the results. I am reading the files one by one by placing the names in other file using while loop my code is as below while read inp do chk=`grep -c "$str" $pth/$inp` ... (2 Replies)
Discussion started by: sekhar gajjala
2 Replies

9. Shell Programming and Scripting

perl how to exit a while loop and quit reading the input file

I am reading a file using While loop while <FILE> { $_ = <FILE>; process data... } I would like to quit reading the file once I encounter a String pattern. How do i do it. is it if (/SUMMARY/) { last; } I am having problems with uninitialized value in pattern... (1 Reply)
Discussion started by: subhap
1 Replies

10. Shell Programming and Scripting

Reading each line of a file in perl script

HI I need to read each line (test.txt) and store it in a array (@test) How to do it in perl. Suppose i have a file test.txt. I have to read each line of the test.txt file and store it in a array @test. How to do it in perl. Regards Harikrishna (3 Replies)
Discussion started by: Harikrishna
3 Replies
Login or Register to Ask a Question

Featured Tech Videos