📄 ch18_03.htm
字号:
<html><head><title>FTP Configuration with Net::Netrc (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch18_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch19_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">18.3. FTP Configuration with Net::Netrc</h2><p><a name="INDEX-2328" /><a name="INDEX-2329" /><a name="INDEX-2330" />Unix-based FTP clients use a file called<em class="emphasis">.netrc</em>, which you can configure to automate FTPaccess to sites you frequently visit. With a properly defined<em class="emphasis">.netrc</em> file, you can simply execute the FTPcommand to a favorite FTP host and be automatically logged in to theFTP server. Your <em class="emphasis">.netrc</em> file contains one linefor each connection you want to make. The following tokens can bespecified for each entry:</p><dl><dt><b><tt class="literal">machine</tt> <em class="replaceable">name</em></b></dt><dd><a name="INDEX-2331" />Specifies a remote machine to which youwant to autologin:</p><blockquote><pre class="code">machine remotehost.com</pre></blockquote><p>Instead of <tt class="literal">machine</tt><em class="replaceable"><tt>name</tt></em>, you can specify the single word<tt class="literal">default</tt> to match any machine name. This is usuallyused for anonymous logins to machines not listed in<em class="emphasis">.netrc</em>.</p></dd><dt><b><tt class="literal">login</tt> <em class="replaceable">name</em></b></dt><dd><a name="INDEX-2332" />Ifpresent, identifies the user who logs in to the remote machine, inwhich <em class="replaceable"><tt>name</tt></em> is the login name.</p></dd><dt><b><tt class="literal">password</tt> <em class="replaceable">passwd</em></b></dt><dd><a name="INDEX-2333" />Ifpresent, gives the password for the user. The autologin process usesthe specified password if the remote server requires one.</p></dd><dt><b><tt class="literal">account</tt> <em class="replaceable">acctpw</em></b></dt><dd><a name="INDEX-2334" />Ifpresent, gives an additional password for the user. The autologinprocess uses this password if the remote server requires one.</p></dd><dt><b><tt class="literal">macdef</tt> <em class="replaceable">name</em></b></dt><dd><a name="INDEX-2335" />Ifpresent, defines a macro with the specified name. Net::Netrc simplyparses this field, to maintain compatibility with FTP.</p></dd></dl><p>Here's an example of a typical<em class="emphasis">.netrc</em> entry:</p><blockquote><pre class="code">machine remotehost.com login username password userpasswd</pre></blockquote><p>Entering your username and password for remote sites in unencryptedform has serious security implications. Many sites consider<em class="emphasis">.netrc</em> files a violation of security policy anddo not allow them. In addition, most FTP clients require that the<em class="emphasis">.netrc</em> file be owned by you and readable by onlyyou, with permissions set to 0400 or 0600. If the permissionsaren't correctly set, the autologin process aborts.Net::Netrc follows this security convention—if the permissionsand ownership are not correct, the <em class="emphasis">.netrc</em> fileisn't read, and a warning message is issued.</p><p>Net::Netrc implements a simple interface to the<em class="emphasis">.netrc</em> file, telling you which hosts you canautomatically connect to. It doesn't actuallyconnect to any remote hosts; it's simply used toquery your configuration file and return the value of the tokens.</p><p>The following methods are supplied by Net::Netrc.</p><a name="INDEX-2336" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>lookup</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">machine</em> = Net::Netrc->lookup(<em class="replaceable">host</em>[, <em class="replaceable">login</em>])</pre><p><a name="INDEX-2336" />Constructor. Looks up and returnsa reference to the entry for the specified host machine<em class="replaceable"><tt>host</tt></em>. Without the optional<em class="replaceable"><tt>login</tt></em> argument, <tt class="literal">lookup</tt>returns the first entry in the <em class="filename">.netrc</em> file forthe machine.</p><p>If there is no matching entry, <tt class="literal">lookup</tt> returns areference to the <tt class="literal">default</tt> entry.</p></div><a name="INDEX-2337" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>account</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">machine</em>->account</pre><p><a name="INDEX-2337" />Returns account informationspecified by the <em class="filename">.netrc</em> account entry, if thereis any.</p></div><a name="INDEX-2338" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>login</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">machine</em>->login</pre><p><a name="INDEX-2338" />Returns the username (login ID)specified by the <em class="filename">.netrc</em> login token.</p></div><a name="INDEX-2339" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>lpa</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>(@<em class="replaceable">login_pass_account</em>) = $<em class="replaceable">machine</em>->lpa</pre><p><a name="INDEX-2339" />Returns a list of login,password, and account information specified by the<em class="filename">.netrc</em> tokens.</p></div><a name="INDEX-2340" /><a name="INDEX-2341" /><a name="INDEX-2342" /><a name="INDEX-2343" /><a name="INDEX-2344" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>password</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">machine</em>->password</pre><p><a name="INDEX-2340" />Returns the password specified by the<em class="filename">.netrc</em> password token.<a name="INDEX-2341" /><a name="INDEX-2342" /><a name="INDEX-2343" /><a name="INDEX-2344" /></p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch18_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch19_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">18.2. Net::FTP</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">19. Lightweight Directory Access with Net::LDAP</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><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 + -