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

📄 ch18_07.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 18.6. Simulating Telnet from a Program (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:05Z"><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_06.htm"TITLE="18.5. Reading Mail with POP3"><LINKREL="next"HREF="ch18_08.htm"TITLE="18.7. Pinging a Machine"></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_06.htm"TITLE="18.5. Reading Mail with POP3"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 18.5. Reading Mail with POP3"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_08.htm"TITLE="18.7. Pinging a Machine"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 18.7. Pinging a Machine"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch18-17527">18.6. Simulating Telnet from a Program</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-845">Problem</A></H3><PCLASS="para"><ACLASS="indexterm"NAME="ch18-idx-1000004315-0"></A><ACLASS="indexterm"NAME="ch18-idx-1000004315-1"></A><ACLASS="indexterm"NAME="ch18-idx-1000004315-2"></A><ACLASS="indexterm"NAME="ch18-idx-1000004315-3"></A>You want to simulate a <EMCLASS="emphasis">telnet</EM> connection from your program by logging into a remote machine, issuing commands, and reacting to what is sent. This has many applications, from automating tasks on machines you can telnet to but which don't support scripting or <EMCLASS="emphasis">rsh</EM>, to simply testing whether a machine's telnet daemon is still running.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-851">Solution</A></H3><PCLASS="para">Use the CPAN module Net::Telnet:</P><PRECLASS="programlisting">use Net::Telnet;$t = Net::Telnet-&gt;new( Timeout =&gt; 10,                       Prompt  =&gt; '/%/',                       Host    =&gt; $hostname );$t-&gt;login($username, $password);@files = $t-&gt;cmd(&quot;ls&quot;);$t-&gt;print(&quot;top&quot;);(undef, $process_string) = $t-&gt;waitfor('/\d+ processes/');$t-&gt;close;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-879">Discussion</A></H3><PCLASS="para">Net::Telnet provides an object-oriented interface to the telnet protocol. Create a connection with <CODECLASS="literal">Net::Telnet-&gt;new</CODE>, and then interact with the remote machine using method calls on the resulting object.</P><PCLASS="para">Give the <CODECLASS="literal">new</CODE> method named parameters, passed in hash-like form. We'll only cover only a few of many possible parameters. The most important is <CODECLASS="literal">Host</CODE>, the machine you're telnetting to. The default host is <CODECLASS="literal">localhost</CODE>. If you want to telnet to a port other than one telnet normally uses, specify this in the <CODECLASS="literal">Port</CODE> option. Error handling is done through the function whose reference is specified in the <CODECLASS="literal">Errmode</CODE> parameter.</P><PCLASS="para">Another important option is <CODECLASS="literal">Prompt</CODE>. When you log in or run a command, Net::Telnet uses the <CODECLASS="literal">Prompt</CODE> pattern to determine when the login or command has completed. The default <CODECLASS="literal">Prompt</CODE> is:</P><PRECLASS="programlisting">/[\$%#&gt;] $/</PRE><PCLASS="para">which matches the common shell prompts. If the prompt on the remote machine doesn't match the default pattern, you have to specify your own. Remember to include the slashes.</P><PCLASS="para"><CODECLASS="literal">Timeout</CODE> lets you control how long (in seconds) network operations wait before they give up. The default is 10 seconds.</P><PCLASS="para">If an error or timeout occurs in the Net::Telnet module, the default behavior is to raise an exception, which, if uncaught, prints a message to STDERR and exits. To change this, pass a subroutine reference to <CODECLASS="literal">new</CODE> in the <CODECLASS="literal">Errmode</CODE> argument. If instead of a code subroutine, you specify the string <CODECLASS="literal">&quot;return&quot;</CODE> as the <CODECLASS="literal">Errmode</CODE>, methods return <CODECLASS="literal">undef</CODE> (in scalar context) or an empty list (in list context) on error, with the error message available via the <CODECLASS="literal">errmsg</CODE> method:</P><PRECLASS="programlisting">$telnet = Net::Telnet-&gt;new( Errmode =&gt; sub { main::log(@_) }, ... );</PRE><PCLASS="para">The <CODECLASS="literal">login</CODE> method is used to send a username and password to the remote machine. It uses the <CODECLASS="literal">Prompt</CODE> to decide when the login is complete and times out if the machine doesn't reply with a prompt:</P><PRECLASS="programlisting">$telnet-&gt;login($username, $password)    or die &quot;Login failed: @{[ $telnet-&gt;errmsg() ]}\n&quot;;</PRE><PCLASS="para">To run a program and gather its output, use the <CODECLASS="literal">cmd</CODE> method. Pass it the string to send, and it returns the output of the command. In list context, it returns one line per list element. In scalar context, it returns one long line. It waits for the <CODECLASS="literal">Prompt</CODE> before returning.</P><PCLASS="para">You can separate the sending of the command from the reception of its output with the <CODECLASS="literal">print</CODE> and <CODECLASS="literal">waitfor</CODE> methods, as we do in the Solution. The <CODECLASS="literal">waitfor</CODE> method takes either a single string containing a Perl regular expression match operator:</P><PRECLASS="programlisting">$telnet-&gt;waitfor('/--more--/')</PRE><PCLASS="para">or named arguments. <CODECLASS="literal">Timeout</CODE> lets you specify a timeout to override the default, <CODECLASS="literal">Match</CODE> is a string containing a match operator as above, and <CODECLASS="literal">String</CODE> is a literal string to find:</P><PRECLASS="programlisting">$telnet-&gt;waitfor(String =&gt; 'greasy smoke', Timeout =&gt; 30)</PRE><PCLASS="para">In scalar context, <CODECLASS="literal">waitfor</CODE> returns true if the pattern or string was found. If it is not found, the <CODECLASS="literal">Errmode</CODE> action is performed. In list context, it returns two strings: all the text before the match, and the text that matched.<ACLASS="indexterm"NAME="ch18-idx-1000004317-0"></A><ACLASS="indexterm"NAME="ch18-idx-1000004317-1"></A><ACLASS="indexterm"NAME="ch18-idx-1000004317-2"></A><ACLASS="indexterm"NAME="ch18-idx-1000004317-3"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-917">See Also</A></H3><PCLASS="para">The documentation for the Net::Telnet module from CPAN; RFCs 854-856, as amended by later RFCs</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_06.htm"TITLE="18.5. Reading Mail with POP3"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 18.5. Reading Mail with POP3"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_08.htm"TITLE="18.7. Pinging a Machine"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 18.7. Pinging a Machine"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">18.5. Reading Mail with POP3</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.7. Pinging a Machine</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 + -