dns querry script.

Discussion in 'Perl Misc' started by Ali Ataman, Jan 6, 2005.

  1. Ali Ataman

    Ali Ataman Guest

    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);
    }
    Ali Ataman, Jan 6, 2005
    #1
    1. Advertising

  2. Ali Ataman

    Paul Lalli Guest

    "Ali Ataman" <> wrote in message
    news:...
    > 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
    Paul Lalli, Jan 6, 2005
    #2
    1. Advertising

  3. Ali Ataman

    Paul Lalli Guest

    "Ali Ataman" <> wrote in message
    news:...
    >
    > #!/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
    Paul Lalli, Jan 6, 2005
    #3
  4. (Ali Ataman) wrote in
    news::

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

    --
    A. Sinan Unur
    d
    (remove '.invalid' and reverse each component for email address)
    A. Sinan Unur, Jan 6, 2005
    #4
  5. On 06 Jan 2005 (Ali Ataman) wrote in
    news::

    > 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.
    }



    --
    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.
    Graham Drabble, Jan 6, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Graham

    feedback form querry

    Graham, Feb 12, 2004, in forum: HTML
    Replies:
    1
    Views:
    426
    Leif K-Brooks
    Feb 12, 2004
  2. SpaceGirl

    Parse querry string with ssi

    SpaceGirl, May 22, 2004, in forum: HTML
    Replies:
    2
    Views:
    1,177
    Martin Johansen
    May 23, 2004
  3. Maulik Thaker

    querry on ID's and IDREF in DTD

    Maulik Thaker, Feb 21, 2006, in forum: XML
    Replies:
    10
    Views:
    1,240
    Aalap
    Feb 25, 2006
  4. Sam Roberts
    Replies:
    0
    Views:
    206
    Sam Roberts
    Mar 20, 2005
  5. gavino
    Replies:
    2
    Views:
    164
    Ted Zlatanov
    Jul 27, 2006
Loading...

Share This Page