Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

problem querying TLD nameserver with Net::DNS::Resolver

by jiemi (Initiate)
on Feb 24, 2011 at 05:05 UTC ( [id://889909]=perlquestion: print w/replies, xml ) Need Help??

jiemi has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple script which I want to use to get the authority records for a domain by querying one of the TLD servers, but when I run my script, it says my variable comes back undefined. I placed my Net::DNS::Resolver in debug mode, and it says I am getting a packet back with authority records, so why is my variable undefined?
#!/usr/local/bin/perl use Net::DNS; $domain = $ARGV[0]; #192.48.79.30 is j.gtld-servers.net my $res = Net::DNS::Resolver->new( nameservers => [qw(192.48.79.30)], recurse => 1, debug => 1, ); $packet = $res->query("$domain", 'NS'); @authority = $packet->authority; foreach $rr (@authority) { print $rr->string, "\n"; }
Output:
whiskey> ./sim5.pl happy.com ;; query(happy.com, NS) ;; setting up an AF_INET() family type UDP socket ;; send_udp(192.35.51.30:53) ;; answer from 192.35.51.30:53 : 105 bytes ;; HEADER SECTION ;; id = 55673 ;; qr = 1 opcode = QUERY aa = 0 tc = 0 rd = 1 ;; ra = 0 ad = 0 cd = 0 rcode = NOERROR ;; qdcount = 1 ancount = 0 nscount = 2 arcount = 2 ;; QUESTION SECTION (1 record) ;; happy.com. IN NS ;; ANSWER SECTION (0 records) ;; AUTHORITY SECTION (2 records) happy.com. 172800 IN NS ns6.walgreens.com. happy.com. 172800 IN NS ns7.walgreens.com. ;; ADDITIONAL SECTION (2 records) ns6.walgreens.com. 172800 IN A 204.15.116.4 ns7.walgreens.com. 172800 IN A 204.15.116.132 Can't call method "authority" on an undefined value at ./sim5.pl line +16.

Replies are listed 'Best First'.
Re: problem querying TLD nameserver with Net::DNS::Resolver
by bingos (Vicar) on Feb 24, 2011 at 10:48 UTC

    Your variable is undefined because there wasn't an answer to the specific question you asked

    From the Net::DNS::Resolver documentation:

    Returns a "Net::DNS::Packet" object, or "undef" if no answers were found. If you need to examine the response packet whether it contains any answers or not, use the send() method instead.

    This amended code uses send() instead:

    use strict; use warnings; use Net::DNS; my $domain = $ARGV[0]; #192.48.79.30 is j.gtld-servers.net my $res = Net::DNS::Resolver->new( nameservers => [qw(192.48.79.30)], recurse => 1, debug => 1, ); my $packet = $res->send("$domain", 'NS'); my @authority = $packet->authority; foreach my $rr (@authority) { print $rr->string, "\n"; }
Re: problem querying TLD nameserver with Net::DNS::Resolver
by Khen1950fx (Canon) on Feb 24, 2011 at 08:47 UTC
    I couldn't get the authority method to work either, but this might be of some help for you:
    #!/usr/local/bin/perl use strict; use warnings; use Net::DNS; use Net::DNS::Resolver::Recurse; check_domain(@ARGV); exit; sub check_domain { my ( $domain, $class ) = @_; $class ||= "IN"; print "-" x 70, "\n"; print "$domain (class $class)\n"; print "\n"; my $res = Net::DNS::Resolver::Recurse->new( timeout => 120, recurse => 1, debug => 1, ); $res->defnames(0); $res->retry(2); my $nspack = $res->query( $domain, "NS", $class ); unless ( defined($nspack) ) { warn "Couldn't find nameservers for $domain: ", $res->errorstr +ing, "\n"; return; } print "nameservers (will request zone from first available):\n"; my $ns; foreach $ns ( grep { $_->type eq "NS" } $nspack->answer ) { print "\t", $ns->nsdname, "\n"; } print "\n"; $res->recursion_callback( sub { my $packet = shift; $_->print for $packet->additional; printf( ";; Received %d bytes from %s\n\n", $packet->answersize, $packet->answerfrom ); } ); $res->query_dorecursion(@ARGV); } exit;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://889909]
Approved by broomduster
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 20:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found