Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

go_names

by jettero (Monsignor)
on Aug 19, 2000 at 18:34 UTC ( [id://28637]=sourcecode: print w/replies, xml ) Need Help??
Category: Networking
Author/Contact Info names@voltar.org
Description: I use this function a lot.
($ip, $hostname) = &go_names( $host );
$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;
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://28637]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2024-04-23 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found