Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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 => '', type => 'A' ); my $fqdn = nslookup ( domain => $ip, type => 'PTR' ); print "$ip -> $fqdn \n";
output: ->  

Debug Output:
Performing 'A' lookup on `' at ./ line 9
Performing 'PTR' lookup on `' at ./ line 11
;; search(, PTR)
;; setting up an AF_INET() family type UDP socket
;; send_udp(
;; answer from : 251 bytes
;; 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)
;;   IN      PTR

;; ANSWER SECTION (1 record)      1166    IN      PTR

;; AUTHORITY SECTION (5 records) 171250  IN      NS 171250  IN      NS 171250  IN      NS 171250  IN      NS 171250  IN      NS

;; ADDITIONAL SECTION (5 records)  51462   IN      A  22807   IN      A  22807   IN      A  22807   IN      A  77787   IN      A ->  

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

    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 => "", 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.

    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://739549]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2018-08-15 22:23 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (165 votes). Check out past polls.