Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > dns querry script.

Reply
Thread Tools

dns querry script.

 
 
Ali Ataman
Guest
Posts: n/a
 
      01-06-2005
Hi everyone.

I have a script that should read a file line by line and make a querry
for each of these domains in the file but it won't work. Can anyone
tell me where the problem might be?

Thanx.



#!/usr/bin/perl -w
$file = '/domlist'; # Name the file
open(INFO, $file); # Open the file
foreach (<INFO>) {

use Net:NS;
my $res = Net:NS::Resolver->new;
my @mx = mx($res,"$_");


if (@mx) {
foreach $rr (@mx) {
print $rr->preference, " ", $rr->exchange, "\n";
}
} else {
warn "Can't find MX records for $_: ", $res->errorstring, "\n";
}
close(INFO);
}
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      01-06-2005
"Ali Ataman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi everyone.
>
> I have a script that should read a file line by line and make a querry
> for each of these domains in the file but it won't work.


That is a remarkably poor error description. What is the expected
output, and how does the actual output differ?

> Can anyone
> tell me where the problem might be?
>
>
>
> #!/usr/bin/perl -w


you forgot
use strict;
and
use warnings;
is better than -w

> $file = '/domlist'; # Name the file
> open(INFO, $file); # Open the file


it is better to use lexical filehandles than bareword filehandles, and
always, always, *always* check the return value of open().
open my $info, $file or die "Cannot open $file: $!";

> foreach (<INFO>) {
>
> use Net:NS;
> my $res = Net:NS::Resolver->new;
> my @mx = mx($res,"$_");


useless use of double-quotes. please see
perldoc -q quoting


> if (@mx) {
> foreach $rr (@mx) {
> print $rr->preference, " ", $rr->exchange, "\n";
> }
> } else {
> warn "Can't find MX records for $_: ", $res->errorstring, "\n";
> }
> close(INFO);
> }


You need to tell us exactly *how* your program isn't working before
anyone can reliably tell you *why* it isn't working.

Paul Lalli

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      01-06-2005
"Ali Ataman" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
>
> #!/usr/bin/perl -w
> $file = '/domlist'; # Name the file
> open(INFO, $file); # Open the file
> foreach (<INFO>) {


My previous advice not-withstanding, I'm willing to bet that the
absolute cause of your error is that you forgot to chomp the domain
here. You're trying to get info about (for example) "amazon.com\n"
instead of "amazon.com". Add the line:

chomp;


Paul Lalli

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-06-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) (Ali Ataman) wrote in
news:(E-Mail Removed) om:

> I have a script that should read a file line by line and make a querry
> for each of these domains in the file but it won't work. Can anyone
> tell me where the problem might be?
>
> Thanx.
>
>
>
> #!/usr/bin/perl -w


use warnings;

is preferable.

Also, you are missing

use strict;

Please read the posting guidelines for this group before going any
further.

> $file = '/domlist'; # Name the file


Useless comments like this are neither necessary nor helpful.

> open(INFO, $file); # Open the file


Always check if open succeeded. Also, I prefer to use the 3-argument form
of open and lexical filehandles:

open my $INFO, '<', $file or die "Cannot open $file: $!";

However, for the purposes of posting in this group, it would be better if
you put some sample data in the __DATA__ section of your script.

> foreach (<INFO>) {


Better to process the file one line at a time:

while(<INFO>) {

> use Net:NS;


There is no point in putting this inside your loop.

> my $res = Net:NS::Resolver->new;
> my @mx = mx($res,"$_");


Useless use of quotes. But more importantly, $_ probably has a newline at
the end.

>
> if (@mx) {
> foreach $rr (@mx) {
> print $rr->preference, " ", $rr->exchange, "\n";
> }
> } else {
> warn "Can't find MX records for $_: ", $res->errorstring, "\n";
> }
> close(INFO);
> }


Always reduce your scripts to the smallest possible program that still
displays the problem. So, for example, if you had tried:

#! /usr/bin/perl

use strict;
use warnings;

use Data:umper;
use Net:NS;

my @mx = mx 'unur.com';
print Dumper \@mx;
__END__

C:\Dload> perl dns.pl
$VAR1 = [
bless( {
'preference' => 5,
'rdlength' => 9,
'ttl' => 86400,
'name' => 'unur.com',
'class' => 'IN',
'type' => 'MX',
'rdata' => ' ??root+?',
'exchange' => 'root.unur.com'
}, 'Net:NS::RR::MX' )
];

and seen that it produces the output you wanted, you would have known
that the problem lay in the reading of domain names from the file as the
following version demonstrates:

#! /usr/bin/perl

use strict;
use warnings;

use Data:umper;
use Net:NS;

while(<DATA>) {
my @mx = mx $_;
print Dumper \@mx;
}

__DATA__
unur.com

C:\Dload> perl dns.pl
$VAR1 = [];
$VAR1 = [];

A few seconds of head scratching would then have enabled you to realize
that you forgot to chomp the data you read:

#! /usr/bin/perl

use strict;
use warnings;

use Data:umper;
use Net:NS;

while(<DATA>) {
chomp;
next unless $_;
my @mx = mx $_;
print Dumper \@mx;
}

__DATA__
unur.com

C:\Dload> perl dns.pl
$VAR1 = [
bless( {
'preference' => 5,
'rdlength' => 9,
'ttl' => 86194,
'name' => 'unur.com',
'class' => 'IN',
'type' => 'MX',
'rdata' => ' ??root+?',
'exchange' => 'root.unur.com'
}, 'Net:NS::RR::MX' )
];

Now, reading the posting guidelines and following the suggestions in that
document would have enabled you to solve the problem on your own. So,
please go ahead, and read that document.

Sinan

--
A. Sinan Unur
(E-Mail Removed)d
(remove '.invalid' and reverse each component for email address)

 
Reply With Quote
 
Graham Drabble
Guest
Posts: n/a
 
      01-06-2005
On 06 Jan 2005 (E-Mail Removed) (Ali Ataman) wrote in
news:(E-Mail Removed) om:

> Hi everyone.
>
> I have a script that should read a file line by line and make a
> querry for each of these domains in the file but it won't work.
> Can anyone tell me where the problem might be?


Doesn't work is a rather poor error message. Problems I've spotted
just looking through quickly are:

#!/usr/bin/perl -w

use strict; #then declare variables using my
use warnings; # is better than -w

# Code is easier to maintain if you indent blocks.

my $file = '/domlist'; # Name the file
open(INFO, $file); # Open the file
foreach (<INFO>) {
use Net:NS; # This should probably be outside the loop.
my $res = Net:NS::Resolver->new;
my @mx = mx($res,"$_");
if (@mx) {
foreach my $rr (@mx) {
print $rr->preference, " ", $rr->exchange, "\n";
}
} else {
warn "Can't find MX records for $_: ", $res->errorstring,
"\n";
}
close(INFO); # You're closing the file having read one line.
# Move this to the end of the script or delete it
# completely as perl will close files for you.
}



--
Graham Drabble
If you're interested in what goes on in other groups or want to find
an interesting group to read then check news.groups.reviews for what
others have to say or contribute a review for others to read.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
querry on ID's and IDREF in DTD Maulik Thaker XML 10 02-25-2006 05:19 PM
querry about compiling and linking gurpreet C Programming 3 02-28-2005 04:30 PM
Parse querry string with ssi SpaceGirl HTML 2 05-23-2004 02:33 AM
feedback form querry Graham HTML 1 02-12-2004 05:11 PM
querry Varsha MCSD 6 01-05-2004 05:34 PM



Advertisments