| Category: | Networking |
| Author/Contact Info | names@voltar.org |
| Description: | I use this function a lot.
$host is either a hostname or an ip ... &go_names attempts to fill $ip and $hostname with an IP and a canonical hostname (using A's and PTR's where possible). Note: This function is desinged for use with names and addresses that forward and reverse lookup the same and that only have one ip address. I mainly use this function for resolving names and IPs in network monitoring apps (that I can't release here). jcwren attempted to do lookups on www.yahoo.com which has 9 or 10 IPs. Worse, www.yahoo.com is a CNAME. I installed no CNAME support at all. Also, please don't use the '-w' with this one. The latest versions of Net::DNS seem to use a bunch of depreciated thingies. |
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->ptrdnam
+e));
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;
}
|
|
|
|---|