dns querry script.

A

Ali Ataman

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::DNS;
my $res = Net::DNS::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);
}
 
P

Paul Lalli

Ali Ataman said:
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::DNS;
my $res = Net::DNS::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
 
P

Paul Lalli

Ali Ataman said:
#!/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
 
A

A. Sinan Unur

(e-mail address removed) (Ali Ataman) wrote in
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( said:
use Net::DNS;

There is no point in putting this inside your loop.
my $res = Net::DNS::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::Dumper;
use Net::DNS;

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::DNS::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::Dumper;
use Net::DNS;

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::Dumper;
use Net::DNS;

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::DNS::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
 
G

Graham Drabble

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::DNS; # This should probably be outside the loop.
my $res = Net::DNS::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.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top