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

How I can do the reverse nslookup? I am looking for working code as Net::Nslookup doesn't work as advertised for this purpose. I am on linux box, but I cannot make gethostbyaddr to work either. I also tried Net::DNS::Resolver unsuccessfully.

Replies are listed 'Best First'.
Re: Reverse nslookup
by ikegami (Patriarch) on Aug 25, 2009 at 20:12 UTC

    In DNS terms, if the ip address is a.b.c.d, you want to lookup the "PTR" (not "A"!) tag for d.c.b.a.in-addr.arpa. That's what gethostbyaddr does:

    use Socket qw( AF_INET inet_aton ); my $name = gethostbyaddr(inet_aton('69.147.76.15'), AF_INET); print "$name\n";
    f1.www.vip.re1.yahoo.com
      Thank you. It works fine.

      I am also trying to populate a table. Can I use time out?

Re: Reverse nslookup
by leighsharpe (Monk) on Aug 25, 2009 at 23:40 UTC
    Net::DNS can do reverse lookups. This was taken almost verbatim from the docs:
    use strict; use warnings; use Net::IP; use Net::DNS; my $ip = new Net::IP($ARGV[0],4); print $ip->reverse_ip()."\n"; print "Resolving ...\n"; my $res = Net::DNS::Resolver->new; my $answer = $res->query($ip->reverse_ip(),'PTR'); my $namer = $answer->{'answer'}[0]; if ($namer->{'ptrdname'}) { print "PTR Name: $namer->{'ptrdname'}\n"; } else { print "N/A\n"; }
    Gives:
    C:\temp>perl revlookup.pl 66.39.54.27 27.54.39.66.in-addr.arpa. Resolving ... PTR Name: ads.perlmonks.org