⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch18_10.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 18.9. Program: expn and vrfy (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:08Z"><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_09.htm"TITLE="18.8. Using Whois to Retrieve Information from the InterNIC"><LINKREL="next"HREF="ch19_01.htm"TITLE="19. CGI Programming"></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_09.htm"TITLE="18.8. Using Whois to Retrieve Information from the InterNIC"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 18.8. Using Whois to Retrieve Information from the InterNIC"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="chapter"HREF="ch19_01.htm"TITLE="19. CGI Programming"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19. CGI Programming"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch18-54471">18.9. Program: expn and vrfy</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch18-idx-1000004337-0"></A><ACLASS="indexterm"NAME="ch18-idx-1000004337-1"></A><ACLASS="indexterm"NAME="ch18-idx-1000004337-2"></A><ACLASS="indexterm"NAME="ch18-idx-1000004337-3"></A>This program talks directly to an SMTP server and uses the EXPN and VRFY commands to figure out whether an address is going to work. It isn't perfect, because it relies on the remote SMTP giving meaningful information with the EXPN and VRFY commands. It uses <ACLASS="indexterm"NAME="ch18-idx-1000004338-0"></A>Net::DNS if available, but can also work without.</P><PCLASS="para">This program inspects <CODECLASS="literal">$0</CODE> (the program name) to see how it was called. If run as <EMCLASS="emphasis">expn</EM>, it uses the EXPN command; if called as <EMCLASS="emphasis">vrfy</EM>, it uses the VRFY command. Use links to install it with two names:</P><PRECLASS="programlisting">% cat &gt; expn#!/usr/bin/perl -w...^D% ln expn vrfy</PRE><PCLASS="para">When you run it with an email address, the program reports what the mail server says when you try to EXPN or VRFY the address. If you have Net::DNS installed, it tries all hosts listed as mail exchangers in the DNS entry for the address.</P><PCLASS="para">Here's what it looks like without Net::DNS:</P><PRECLASS="programlisting">% expn gnat@frii.com<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Expanding gnat at frii.com (gnat@frii.com):</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>calisto.frii.com Hello coprolith.frii.com [207.46.130.14],</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    pleased to meet you</I></CODE></B></CODE><BCLASS="emphasis.bold">&lt;gnat@mail.frii.com&gt;</B></PRE><PCLASS="para">And here's the same address with Net::DNS installed:</P><PRECLASS="programlisting">% expn gnat@frii.com<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Expanding gnat at mail.frii.net (gnat@frii.com):</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>deimos.frii.com Hello coprolith.frii.com [207.46.130.14],</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    pleased to meet you</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Nathan Torkington &lt;gnat@deimos.frii.com&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Expanding gnat at mx1.frii.net (gnat@frii.com):</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>phobos.frii.com Hello coprolith.frii.com [207.46.130.14],</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    pleased to meet you</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;gnat@mail.frii.com&gt;</I></CODE></B></CODE><BCLASS="emphasis.bold">E</B><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>xpanding gnat at mx2.frii.net (gnat@frii.com):</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>europa.frii.com Hello coprolith.frii.com [207.46.130.14],</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    pleased to meet you</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;gnat@mail.frii.com&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>Expanding gnat at mx3.frii.net (gnat@frii.com):</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>ns2.winterlan.com Hello coprolith.frii.com [207.46.130.14],</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    pleased to meet you</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>550 gnat... User unknown</I></CODE></B></CODE></PRE><PCLASS="para">The program is shown in <ACLASS="xref"HREF="ch18_10.htm#ch18-37121"TITLE="expn">Example 18.3</A>.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch18-37121">Example 18.3: expn</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# expn -- convince smtp to divulge an alias expansionuse strict;use IO::Socket;use Sys::Hostname;my $fetch_mx = 0;# try loading the module, but don't blow up if missingeval {        require Net::DNS;    Net::DNS-&gt;import(<CODECLASS="literal">'</CODE>mx');    $fetch_mx = 1;};my $selfname = hostname();die &quot;usage: $0 address\@host ...\n&quot; unless @ARGV;# Find out whether called as &quot;vrfy&quot; or &quot;expn&quot;.my $VERB = ($0 =~ /ve?ri?fy$/i)  ? 'VRFY' : 'EXPN';my $multi = @ARGV &gt; 1;my $remote;# Iterate over addresses give on command line.foreach my $combo (@ARGV) {    my ($name, $host) = split(/\@/, $combo);    my @hosts;    $host ||= 'localhost';    @hosts = map { $_-&gt;exchange } mx($host)     if $fetch_mx;    @hosts = ($host)   unless @hosts;    foreach my $host (@hosts) {         print $VERB eq 'VRFY' ? &quot;Verify&quot; : &quot;Expand&quot;,             &quot;ing $name at $host ($combo):&quot;;        $remote = IO::Socket::INET-&gt;new(               Proto    =&gt; &quot;tcp&quot;,               PeerAddr =&gt; $host,               PeerPort =&gt; &quot;smtp(25)&quot;,           );         unless ($remote) {             warn &quot;cannot connect to $host\n&quot;;            next;        }        print &quot;\n&quot;;        $remote-&gt;autoflush(1);        # use CRLF network line terminators        print $remote &quot;HELO $selfname\015\012&quot;;        print $remote &quot;$VERB $name\015\012&quot;;        print $remote &quot;quit\015\012&quot;;        while (&lt;$remote&gt;) {            /^220\b/ &amp;&amp; next;            /^221\b/ &amp;&amp; last;            s/250\b[\-\s]+//;            print;        }         close($remote) or die &quot;can't close socket: $!&quot;;        print &quot;\n&quot;; #  if @ARGV;    }<ACLASS="indexterm"NAME="ch18-idx-1000004340-0"></A><ACLASS="indexterm"NAME="ch18-idx-1000004340-1"></A><ACLASS="indexterm"NAME="ch18-idx-1000004340-2"></A><ACLASS="indexterm"NAME="ch18-idx-1000004340-3"></A>} <ACLASS="indexterm"NAME="ch18-idx-1000004268-0"></A></PRE></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_09.htm"TITLE="18.8. Using Whois to Retrieve Information from the InterNIC"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 18.8. Using Whois to Retrieve Information from the InterNIC"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="chapter"HREF="ch19_01.htm"TITLE="19. CGI Programming"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19. CGI Programming"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">18.8. Using Whois to Retrieve Information from the InterNIC</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">19. CGI Programming</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 &copy; 2002</a> O'Reilly &amp; 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 + -