Reverse DNS lookup in perl

If you need more detailed DNS info use the Net::DNS module, here is an example:

use Net::DNS;
my $res = Net::DNS::Resolver->new;

# create the reverse lookup DNS name (note that the octets in the IP address need to be reversed).
my $IP = "209.85.173.103";
my $target_IP = join('.', reverse split(/\./, $IP)).".in-addr.arpa";

my $query = $res->query("$target_IP", "PTR");

if ($query) {
  foreach my $rr ($query->answer) {
    next unless $rr->type eq "PTR";
    print $rr->rdatastr, "\n";
  }
} else {
  warn "query failed: ", $res->errorstring, "\n";
}

Original Source EliteHackers.info, more details there as well.


use Socket;
$iaddr = inet_aton("127.0.0.1"); # or whatever address
$name  = gethostbyaddr($iaddr, AF_INET);

gethostbyaddr and similar calls. See http://perldoc.perl.org/functions/gethostbyaddr.html