Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Net::Nslookup PTR lookup help

by blackstealth (Novice)
on Jan 28, 2009 at 13:30 UTC ( #739549=perlquestion: print w/replies, xml ) Need Help??
blackstealth has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks i come to you in a bit of a pickle. I have been asked to parse some access logs and give feedback on who is using a utility ( the logs are very apache like) i have broken it down to where i can get day and ip and all that but they didn't like having the "ip address" cause they don't know what that ip is so i am using Net::Nslookup. I can get type => 'A' records and have turned on all the debugging and the dns is returning but i am not getting a value back i have even thought of the fact that it may be an arrary and haven't found anything there either. I have even created some test code and it does not work. I will use the test code to post here as it is much smaller and has the same issue:
#!/usr/bin/perl use strict; use Net::Nslookup; my $ip = nslookup( domain => 'www.yahoo.com', type => 'A' ); my $fqdn = nslookup ( domain => $ip, type => 'PTR' ); print "$ip -> $fqdn \n";
output:
69.147.76.15 ->  

Debug Output:
Performing 'A' lookup on `www.yahoo.com' at ./test.pl line 9
Performing 'PTR' lookup on `69.147.76.15' at ./test.pl line 11
;; search(69.147.76.15, PTR)
;; setting up an AF_INET() family type UDP socket
;; send_udp(24.28.201.85:53)
;; answer from 24.28.201.85:53 : 251 bytes
;; HEADER SECTION
;; id = 46054
;; qr = 1    opcode = QUERY    aa = 0    tc = 0    rd = 1
;; ra = 1    ad = 0    cd = 0    rcode  = NOERROR
;; qdcount = 1  ancount = 1  nscount = 5  arcount = 5

;; QUESTION SECTION (1 record)
;; 15.76.147.69.in-addr.arpa.   IN      PTR

;; ANSWER SECTION (1 record)
15.76.147.69.in-addr.arpa.      1166    IN      PTR     f1.www.vip.re1.yahoo.com.

;; AUTHORITY SECTION (5 records)
76.147.69.in-addr.arpa. 171250  IN      NS      ns2.yahoo.com.
76.147.69.in-addr.arpa. 171250  IN      NS      ns3.yahoo.com.
76.147.69.in-addr.arpa. 171250  IN      NS      ns4.yahoo.com.
76.147.69.in-addr.arpa. 171250  IN      NS      ns5.yahoo.com.
76.147.69.in-addr.arpa. 171250  IN      NS      ns1.yahoo.com.

;; ADDITIONAL SECTION (5 records)
ns5.yahoo.com.  51462   IN      A       119.160.247.124
ns4.yahoo.com.  22807   IN      A       68.142.196.63
ns3.yahoo.com.  22807   IN      A       217.12.4.104
ns2.yahoo.com.  22807   IN      A       68.142.255.16
ns1.yahoo.com.  77787   IN      A       68.180.131.16

69.147.76.15 ->  

Replies are listed 'Best First'.
Re: Net::Nslookup PTR lookup help (use Net::DNS)
by grinder (Bishop) on Jan 28, 2009 at 13:52 UTC

    Personally, I find Net::DNS much easier to use. Consider:

    #! /usr/bin/perl -w use strict; use Net::DNS; my $r = Net::DNS::Resolver->new; for my $ip (@ARGV) { my $reverse = join( '.', reverse( split /\./, $ip )) . '.in-addr.a +rpa'; if (my $ap = $r->query( $reverse, 'PTR' )) { for my $pa ($ap->answer) { print "$ip => ", $pa->ptrdname, $/; } } else { print "$ip => NXDOMAIN\n"; } } __OUTPUT__ ./ptr 69.147.76.15 69.147.76.15 => f1.www.vip.re1.yahoo.com

    Update: easier... hmmm... let me rephrase that: "gives me finer control over what I want to do". It's a really useful module to have in your toolkit if you ever have to grovel around with CNAME, MX, NS, SOA records and the like.

    2nd update: new improved! with error checking!

    • another intruder with the mooring in the heart of the Perl

      Thank you very much i have tired that out and it works.
Re: Net::Nslookup PTR lookup help
by kubrat (Scribe) on Jan 28, 2009 at 13:50 UTC

    Try this:

    my $name = nslookup(host => "206.33.105.41", type => "PTR");

    Uses 'host' for a key in the PTR query instead of 'domain'. I just copied the example provided in the module's documentation.

    http://search.cpan.org/~darren/Net-Nslookup-1.18/lib/Net/Nslookup.pm

    Update: It doesn't work for me either. This module is just a wrapper for Net::DNS. You can fix the problem by deleting an if statement in its _lookup_ptr function.

    If you substitute the following ...

    $query = $res->search($term, "PTR") || return; for $rr ($query->answer) { if ($rr->can('ptrdname')) { push @answers, $rr->ptrdname; } }

    with ...

    $query = $res->search($term, "PTR") || return; for $rr ($query->answer) { push @answers, $rr->ptrdname; }

    then it works as expected :)

      All to unfortunately it still doesn't work from reading the documentation it was my understanding that i could use "host", "domain", or "term" all interchangeably :( but still no luck.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://739549]
Approved by Corion
help
Chatterbox?
[Corion]: A pleasant daypart to everybody!

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2018-05-23 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?