use Net::DNS; my %name_cache = (); sub go_names { my $host = shift; my $already = shift; return @{ $name_cache{$host} } if defined $name_cache{$host}; my $resolver = new Net::DNS::Resolver; my $query = $resolver->search($host) or return (undef, undef); my @answer = $query->answer or return (undef, undef); my $rr = shift @answer; my $is_ip = $rr->type eq "A"; my $is_ptr = $rr->type eq "PTR"; return (undef, undef) if not ($is_ip or $is_ptr); my @ret = (($is_ip) ? ($rr->address, $host) : ($host, $rr->ptrdname)); return &go_names($ret[0], 1) if $ret[1] !~ /[.]/ and not $already; # canonize iff needed and not already recursing. $name_cache{$host} = [ @ret ]; return @ret; }