Perl Cookbook

Perl CookbookSearch this book
Previous: 18.7. Pinging a MachineChapter 18
Internet Services
Next: 18.9. Program: expn and vrfy
 

18.8. Using Whois to Retrieve Information from the InterNIC

Problem

You want to find out who owns a domain, as if you'd used the Unix whois command.

Solution

Use the CPAN module Net::Whois:

use Net::Whois;

$domain_obj = Net::Whois::Domain->new($domain_name)
    or die "Couldn't get information on $domain_name: $!\n";

# call methods on $domain_obj to get name, tag, address, etc.

Discussion

Whois is a service provided by domain name registration authorities to identify owners of domain names. Historically, queries were made with the whois (1) program on Unix systems, which returned about fifteen lines of information, including the names, addresses, and phone numbers of the administrative, technical, and billing contacts for the domain.

The Net::Whois module is a client for the whois service, just like whois (1). It connects to a whois server (the default is whois.internic.net, the master server for the ".com", ".org", ".net", and ".edu" domains) and gives you access to the information through method calls on an object.

To request information on a domain, create a new Net::Whois::Domain object. For instance, to look up information on perl.org:

$d = Net::Whois::Domain->new( "perl.org" )
    or die "Can't get information on perl.org\n";

The only guaranteed fields are the domain name and the tag  - the domain's unique identifier in the NIC records:

print "The domain is called ", $d->domain, "\n";
print "Its tag is ", $d->tag, "\n";

Information that may be present includes: name of the domain's company or product (e.g., "The Perl Institute"), the address of the company (a list of lines, e.g., ("221B Baker Street", "London")), and the country the address is valid for (e.g., "United Kingdom" or its two-letter abbreviation "uk").

print "Mail for ", $d->name, " should be sent to:\n";
print map { "\t$_\n" } $d->address;
print "\t", $d->country, "\n";

In addition to information about the domain, you can also get information on the domain's contacts. The contact method returns a reference to a hash mapping contact type (e.g., "Billing" or "Administrative") onto an array of lines.

$contact_hash = $d->contacts;
if ($contact_hash) {
    print "Contacts:\n";
    foreach $type (sort keys %$contact_hash) {
        print "  $type:\n";
        foreach $line (@{$contact_hash->{$type}}) {
            print "    $line\n";
        }
    }
} else {
    print "No contact information.\n";
}

See Also

The documentation for the Net::Whois module from CPAN; your system's whois (8) manpage (if you have one); RFC 812 and 954


Previous: 18.7. Pinging a MachinePerl CookbookNext: 18.9. Program: expn and vrfy
18.7. Pinging a MachineBook Index18.9. Program: expn and vrfy

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.