Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse DNS lookup in perl

How do I perform a reverse DNS lookup, that is how do I resolve an IP address to its DNS hostname in Perl?

like image 473
Joakim Avatar asked Sep 17 '08 17:09

Joakim


1 Answers

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.

like image 150
Jeff Avatar answered Sep 26 '22 08:09

Jeff