📄 ch18_09.htm
字号:
<HTML><HEAD><TITLE>Recipe 18.8. Using Whois to Retrieve Information from the InterNIC (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:07Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch18_01.htm"TITLE="18. Internet Services"><LINKREL="prev"HREF="ch18_08.htm"TITLE="18.7. Pinging a Machine"><LINKREL="next"HREF="ch18_10.htm"TITLE="18.9. Program: expn and vrfy"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_08.htm"TITLE="18.7. Pinging a Machine"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 18.7. Pinging a Machine"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch18_01.htm"TITLE="18. Internet Services"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_10.htm"TITLE="18.9. Program: expn and vrfy"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 18.9. Program: expn and vrfy"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch18-chap18_using_0">18.8. Using Whois to Retrieve Information from the InterNIC</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-995">Problem<ACLASS="indexterm"NAME="ch18-idx-1000004329-0"></A><ACLASS="indexterm"NAME="ch18-idx-1000004329-1"></A><ACLASS="indexterm"NAME="ch18-idx-1000004329-2"></A><ACLASS="indexterm"NAME="ch18-idx-1000004329-3"></A></A></H3><PCLASS="para">You want to find out who owns a domain, as if you'd used the Unix <CODECLASS="literal">whois</CODE> command.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-1001">Solution</A></H3><PCLASS="para">Use the CPAN module Net::Whois:</P><PRECLASS="programlisting">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.</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-1019">Discussion</A></H3><PCLASS="para">Whois is a service provided by domain name registration authorities to identify owners of domain names. Historically, queries were made with the <ICLASS="filename">whois </I>(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.</P><PCLASS="para">The <CODECLASS="literal">Net::Whois</CODE> module is a client for the whois service, just like <ICLASS="filename">whois</I> (1). It connects to a whois server (the default is <CODECLASS="literal">whois.internic.net</CODE>, the master server for the <CODECLASS="literal">".com"</CODE>, <CODECLASS="literal">".org"</CODE>, <CODECLASS="literal">".net"</CODE>, and <CODECLASS="literal">".edu"</CODE> domains) and gives you access to the information through method calls on an object.</P><PCLASS="para">To request information on a domain, create a new Net::Whois::Domain object. For instance, to look up information on <CODECLASS="literal">perl.org</CODE>:</P><PRECLASS="programlisting">$d = Net::Whois::Domain->new( "perl.org" ) or die "Can't get information on perl.org\n";</PRE><PCLASS="para">The only guaranteed fields are the domain name and the tag - the domain's unique identifier in the NIC records:</P><PRECLASS="programlisting">print "The domain is called ", $d->domain, "\n";print "Its tag is ", $d->tag, "\n";</PRE><PCLASS="para">Information that may be present includes: <EMCLASS="emphasis">name</EM> of the domain's company or product (e.g., "<CODECLASS="literal">The</CODE> <CODECLASS="literal">Perl</CODE> <CODECLASS="literal">Institute</CODE>"), the <EMCLASS="emphasis">address</EM> of the company (a list of lines, e.g., <CODECLASS="literal">("221B</CODE> <CODECLASS="literal">Baker</CODE> <CODECLASS="literal">Street",</CODE> <CODECLASS="literal">"London")</CODE>), and the <EMCLASS="emphasis">country</EM> the address is valid for (e.g., "<CODECLASS="literal">United</CODE> <CODECLASS="literal">Kingdom</CODE>" or its two-letter abbreviation "<CODECLASS="literal">uk</CODE>").</P><PRECLASS="programlisting">print "Mail for ", $d->name, " should be sent to:\n";print map { "\t$_\n" } $d->address;print "\t", $d->country, "\n";</PRE><PCLASS="para">In addition to information about the domain, you can also get information on the domain's <EMCLASS="emphasis">contacts</EM>. The <CODECLASS="literal">contact</CODE> method returns a reference to a hash mapping contact type (e.g., <CODECLASS="literal">"Billing"</CODE> or <CODECLASS="literal">"Administrative"</CODE>) onto an array of lines.</P><PRECLASS="programlisting">$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";}<ACLASS="indexterm"NAME="ch18-idx-1000004331-0"></A><ACLASS="indexterm"NAME="ch18-idx-1000004331-1"></A><ACLASS="indexterm"NAME="ch18-idx-1000004331-2"></A><ACLASS="indexterm"NAME="ch18-idx-1000004331-3"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-1073">See Also</A></H3><PCLASS="para">The documentation for the Net::Whois module from CPAN; your system's <ICLASS="filename">whois </I>(8) manpage (if you have one); RFC 812 and 954</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_08.htm"TITLE="18.7. Pinging a Machine"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 18.7. Pinging a Machine"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_10.htm"TITLE="18.9. Program: expn and vrfy"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 18.9. Program: expn and vrfy"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">18.7. Pinging a Machine</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">18.9. Program: expn and vrfy</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -