文件操作 - pxssh.html
返回文件管理
返回主菜单
删除本文件
文件: /usr/share/doc/pexpect-2.3/doc/pxssh.html
编辑文件内容
<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><title>Python: module pxssh</title> </head><body bgcolor="#f0f0f8"> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading"> <tr bgcolor="#7799ee"> <td valign=bottom> <br> <font color="#ffffff" face="helvetica, arial"> <br><big><big><strong>pxssh</strong></big></big> (version 2.3)</font></td ><td align=right valign=bottom ><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/home/noah/pexpect/trunk/pexpect/pxssh.py">/home/noah/pexpect/trunk/pexpect/pxssh.py</a></font></td></tr></table> <p><tt>This class extends pexpect.<a href="pexpect.html#spawn">spawn</a> to specialize setting up SSH connections.<br> This adds methods for login, logout, and expecting the shell prompt.<br> <br> $Id: <a href="#pxssh">pxssh</a>.py 487 2007-08-29 22:33:29Z noah $</tt></p> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#aa55cc"> <td colspan=3 valign=bottom> <br> <font color="#fffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr> <tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td> <td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="pexpect.html">pexpect</a><br> </td><td width="25%" valign=top><a href="time.html">time</a><br> </td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ee77aa"> <td colspan=3 valign=bottom> <br> <font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr> <tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td> <td width="100%"><dl> <dt><font face="helvetica, arial"><a href="pexpect.html#ExceptionPexpect">pexpect.ExceptionPexpect</a>(<a href="exceptions.html#Exception">exceptions.Exception</a>) </font></dt><dd> <dl> <dt><font face="helvetica, arial"><a href="pxssh.html#ExceptionPxssh">ExceptionPxssh</a> </font></dt></dl> </dd> <dt><font face="helvetica, arial"><a href="pexpect.html#spawn">pexpect.spawn</a>(<a href="__builtin__.html#object">__builtin__.object</a>) </font></dt><dd> <dl> <dt><font face="helvetica, arial"><a href="pxssh.html#pxssh">pxssh</a> </font></dt></dl> </dd> </dl> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ffc8d8"> <td colspan=3 valign=bottom> <br> <font color="#000000" face="helvetica, arial"><a name="ExceptionPxssh">class <strong>ExceptionPxssh</strong></a>(<a href="pexpect.html#ExceptionPexpect">pexpect.ExceptionPexpect</a>)</font></td></tr> <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> <td colspan=2><tt>Raised for <a href="#pxssh">pxssh</a> exceptions.<br> </tt></td></tr> <tr><td> </td> <td width="100%"><dl><dt>Method resolution order:</dt> <dd><a href="pxssh.html#ExceptionPxssh">ExceptionPxssh</a></dd> <dd><a href="pexpect.html#ExceptionPexpect">pexpect.ExceptionPexpect</a></dd> <dd><a href="exceptions.html#Exception">exceptions.Exception</a></dd> <dd><a href="exceptions.html#BaseException">exceptions.BaseException</a></dd> <dd><a href="__builtin__.html#object">__builtin__.object</a></dd> </dl> <hr> Methods inherited from <a href="pexpect.html#ExceptionPexpect">pexpect.ExceptionPexpect</a>:<br> <dl><dt><a name="ExceptionPxssh-__init__"><strong>__init__</strong></a>(self, value)</dt></dl> <dl><dt><a name="ExceptionPxssh-__str__"><strong>__str__</strong></a>(self)</dt></dl> <dl><dt><a name="ExceptionPxssh-get_trace"><strong>get_trace</strong></a>(self)</dt><dd><tt>This returns an abbreviated stack trace with lines that only concern<br> the caller. In other words, the stack trace inside the Pexpect module<br> is not included.</tt></dd></dl> <hr> Data descriptors inherited from <a href="pexpect.html#ExceptionPexpect">pexpect.ExceptionPexpect</a>:<br> <dl><dt><strong>__weakref__</strong></dt> <dd><tt>list of weak references to the object (if defined)</tt></dd> </dl> <hr> Data and other attributes inherited from <a href="exceptions.html#Exception">exceptions.Exception</a>:<br> <dl><dt><strong>__new__</strong> = <built-in method __new__ of type object at 0x81400e0><dd><tt>T.<a href="#ExceptionPxssh-__new__">__new__</a>(S, ...) -> a new object with type S, a subtype of T</tt></dl> <hr> Methods inherited from <a href="exceptions.html#BaseException">exceptions.BaseException</a>:<br> <dl><dt><a name="ExceptionPxssh-__delattr__"><strong>__delattr__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionPxssh-__delattr__">__delattr__</a>('name') <==> del x.name</tt></dd></dl> <dl><dt><a name="ExceptionPxssh-__getattribute__"><strong>__getattribute__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionPxssh-__getattribute__">__getattribute__</a>('name') <==> x.name</tt></dd></dl> <dl><dt><a name="ExceptionPxssh-__getitem__"><strong>__getitem__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionPxssh-__getitem__">__getitem__</a>(y) <==> x[y]</tt></dd></dl> <dl><dt><a name="ExceptionPxssh-__getslice__"><strong>__getslice__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionPxssh-__getslice__">__getslice__</a>(i, j) <==> x[i:j]<br> <br> Use of negative indices is not supported.</tt></dd></dl> <dl><dt><a name="ExceptionPxssh-__reduce__"><strong>__reduce__</strong></a>(...)</dt></dl> <dl><dt><a name="ExceptionPxssh-__repr__"><strong>__repr__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionPxssh-__repr__">__repr__</a>() <==> repr(x)</tt></dd></dl> <dl><dt><a name="ExceptionPxssh-__setattr__"><strong>__setattr__</strong></a>(...)</dt><dd><tt>x.<a href="#ExceptionPxssh-__setattr__">__setattr__</a>('name', value) <==> x.name = value</tt></dd></dl> <dl><dt><a name="ExceptionPxssh-__setstate__"><strong>__setstate__</strong></a>(...)</dt></dl> <hr> Data descriptors inherited from <a href="exceptions.html#BaseException">exceptions.BaseException</a>:<br> <dl><dt><strong>__dict__</strong></dt> </dl> <dl><dt><strong>args</strong></dt> </dl> <dl><dt><strong>message</strong></dt> <dd><tt>exception message</tt></dd> </dl> </td></tr></table> <p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#ffc8d8"> <td colspan=3 valign=bottom> <br> <font color="#000000" face="helvetica, arial"><a name="pxssh">class <strong>pxssh</strong></a>(<a href="pexpect.html#spawn">pexpect.spawn</a>)</font></td></tr> <tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td> <td colspan=2><tt>This class extends pexpect.<a href="pexpect.html#spawn">spawn</a> to specialize setting up SSH<br> connections. This adds methods for login, logout, and expecting the shell<br> prompt. It does various tricky things to handle many situations in the SSH<br> login process. For example, if the session is your first login, then <a href="#pxssh">pxssh</a><br> automatically accepts the remote certificate; or if you have public key<br> authentication setup then <a href="#pxssh">pxssh</a> won't wait for the password prompt.<br> <br> <a href="#pxssh">pxssh</a> uses the shell prompt to synchronize output from the remote host. In<br> order to make this more robust it sets the shell prompt to something more<br> unique than just $ or #. This should work on most Borne/Bash or Csh style<br> shells.<br> <br> Example that runs a few commands on a remote server and prints the result::<br> <br> import <a href="#pxssh">pxssh</a><br> import getpass<br> try: <br> s = <a href="#pxssh">pxssh</a>.<a href="#pxssh">pxssh</a>()<br> hostname = raw_input('hostname: ')<br> username = raw_input('username: ')<br> password = getpass.getpass('password: ')<br> s.login (hostname, username, password)<br> s.sendline ('uptime') # run a command<br> s.<a href="#pxssh-prompt">prompt</a>() # match the prompt<br> print s.before # print everything before the prompt.<br> s.sendline ('ls -l')<br> s.<a href="#pxssh-prompt">prompt</a>()<br> print s.before<br> s.sendline ('df')<br> s.<a href="#pxssh-prompt">prompt</a>()<br> print s.before<br> s.<a href="#pxssh-logout">logout</a>()<br> except <a href="#pxssh">pxssh</a>.<a href="#ExceptionPxssh">ExceptionPxssh</a>, e:<br> print "<a href="#pxssh">pxssh</a> failed on login."<br> print str(e)<br> <br> Note that if you have ssh-agent running while doing development with <a href="#pxssh">pxssh</a><br> then this can lead to a lot of confusion. Many X display managers (xdm,<br> gdm, kdm, etc.) will automatically start a GUI agent. You may see a GUI<br> dialog box popup asking for a password during development. You should turn<br> off any key agents during testing. The 'force_password' attribute will turn<br> off public key authentication. This will only work if the remote SSH server<br> is configured to allow password logins. Example of using 'force_password'<br> attribute::<br> <br> s = <a href="#pxssh">pxssh</a>.<a href="#pxssh">pxssh</a>()<br> s.force_password = True<br> hostname = raw_input('hostname: ')<br> username = raw_input('username: ')<br> password = getpass.getpass('password: ')<br> s.login (hostname, username, password)<br> </tt></td></tr> <tr><td> </td> <td width="100%"><dl><dt>Method resolution order:</dt> <dd><a href="pxssh.html#pxssh">pxssh</a></dd> <dd><a href="pexpect.html#spawn">pexpect.spawn</a></dd> <dd><a href="__builtin__.html#object">__builtin__.object</a></dd> </dl> <hr> Methods defined here:<br> <dl><dt><a name="pxssh-__init__"><strong>__init__</strong></a>(self, timeout<font color="#909090">=30</font>, maxread<font color="#909090">=2000</font>, searchwindowsize<font color="#909090">=None</font>, logfile<font color="#909090">=None</font>, cwd<font color="#909090">=None</font>, env<font color="#909090">=None</font>)</dt></dl> <dl><dt><a name="pxssh-levenshtein_distance"><strong>levenshtein_distance</strong></a>(self, a, b)</dt><dd><tt>This calculates the Levenshtein distance between a and b.</tt></dd></dl> <dl><dt><a name="pxssh-login"><strong>login</strong></a>(self, server, username, password<font color="#909090">=''</font>, terminal_type<font color="#909090">='ansi'</font>, original_prompt<font color="#909090">='[#$]'</font>, login_timeout<font color="#909090">=10</font>, port<font color="#909090">=None</font>, auto_prompt_reset<font color="#909090">=True</font>)</dt><dd><tt>This logs the user into the given server. It uses the<br> 'original_prompt' to try to find the prompt right after login. When it<br> finds the prompt it immediately tries to reset the prompt to something<br> more easily matched. The default 'original_prompt' is very optimistic<br> and is easily fooled. It's more reliable to try to match the original<br> prompt as exactly as possible to prevent false matches by server<br> strings such as the "Message Of The Day". On many systems you can<br> disable the MOTD on the remote server by creating a zero-length file<br> called "~/.hushlogin" on the remote server. If a prompt cannot be found<br> then this will not necessarily cause the login to fail. In the case of<br> a timeout when looking for the prompt we assume that the original<br> prompt was so weird that we could not match it, so we use a few tricks<br> to guess when we have reached the prompt. Then we hope for the best and<br> blindly try to reset the prompt to something more unique. If that fails<br> then <a href="#pxssh-login">login</a>() raises an <a href="#ExceptionPxssh">ExceptionPxssh</a> exception.<br> <br> In some situations it is not possible or desirable to reset the<br> original prompt. In this case, set 'auto_prompt_reset' to False to<br> inhibit setting the prompt to the UNIQUE_PROMPT. Remember that <a href="#pxssh">pxssh</a><br> uses a unique prompt in the <a href="#pxssh-prompt">prompt</a>() method. If the original prompt is<br> not reset then this will disable the <a href="#pxssh-prompt">prompt</a>() method unless you<br> manually set the PROMPT attribute.</tt></dd></dl> <dl><dt><a name="pxssh-logout"><strong>logout</strong></a>(self)</dt><dd><tt>This sends exit to the remote shell. If there are stopped jobs then<br> this automatically sends exit twice.</tt></dd></dl> <dl><dt><a name="pxssh-prompt"><strong>prompt</strong></a>(self, timeout<font color="#909090">=20</font>)</dt><dd><tt>This matches the shell prompt. This is little more than a short-cut<br> to the <a href="#pxssh-expect">expect</a>() method. This returns True if the shell prompt was<br> matched. This returns False if there was a timeout. Note that if you<br> called <a href="#pxssh-login">login</a>() with auto_prompt_reset set to False then you should have<br> manually set the PROMPT attribute to a regex pattern for matching the<br> prompt.</tt></dd></dl> <dl><dt><a name="pxssh-set_unique_prompt"><strong>set_unique_prompt</strong></a>(self)</dt><dd><tt>This sets the remote prompt to something more unique than # or $.<br> This makes it easier for the <a href="#pxssh-prompt">prompt</a>() method to match the shell prompt<br> unambiguously. This method is called automatically by the <a href="#pxssh-login">login</a>()<br> method, but you may want to call it manually if you somehow reset the<br> shell prompt. For example, if you 'su' to a different user then you<br> will need to manually reset the prompt. This sends shell commands to<br> the remote host to set the prompt, so this assumes the remote host is<br> ready to receive commands.<br> <br> Alternatively, you may use your own prompt pattern. Just set the PROMPT<br> attribute to a regular expression that matches it. In this case you<br> should call <a href="#pxssh-login">login</a>() with auto_prompt_reset=False; then set the PROMPT<br> attribute. After that the <a href="#pxssh-prompt">prompt</a>() method will try to match your prompt<br> pattern.</tt></dd></dl> <dl><dt><a name="pxssh-synch_original_prompt"><strong>synch_original_prompt</strong></a>(self)</dt><dd><tt>This attempts to find the prompt. Basically, press enter and record<br> the response; press enter again and record the response; if the two<br> responses are similar then assume we are at the original prompt.</tt></dd></dl> <hr> Methods inherited from <a href="pexpect.html#spawn">pexpect.spawn</a>:<br> <dl><dt><a name="pxssh-__del__"><strong>__del__</strong></a>(self)</dt><dd><tt>This makes sure that no system resources are left open. Python only<br> garbage collects Python objects. OS file descriptors are not Python<br> objects, so they must be handled explicitly. If the child file<br> descriptor was opened outside of this class (passed to the constructor)<br> then this does not close it.</tt></dd></dl> <dl><dt><a name="pxssh-__iter__"><strong>__iter__</strong></a>(self)</dt><dd><tt>This is to support iterators over a file-like object.</tt></dd></dl> <dl><dt><a name="pxssh-__str__"><strong>__str__</strong></a>(self)</dt><dd><tt>This returns a human-readable string that represents the state of<br> the object.</tt></dd></dl> <dl><dt><a name="pxssh-close"><strong>close</strong></a>(self, force<font color="#909090">=True</font>)</dt><dd><tt>This closes the connection with the child application. Note that<br> calling <a href="#pxssh-close">close</a>() more than once is valid. This emulates standard Python<br> behavior with files. Set force to True if you want to make sure that<br> the child is terminated (SIGKILL is sent if the child ignores SIGHUP<br> and SIGINT).</tt></dd></dl> <dl><dt><a name="pxssh-compile_pattern_list"><strong>compile_pattern_list</strong></a>(self, patterns)</dt><dd><tt>This compiles a pattern-string or a list of pattern-strings.<br> Patterns must be a StringType, EOF, TIMEOUT, SRE_Pattern, or a list of<br> those. Patterns may also be None which results in an empty list (you<br> might do this if waiting for an EOF or TIMEOUT condition without<br> expecting any pattern).<br> <br> This is used by <a href="#pxssh-expect">expect</a>() when calling <a href="#pxssh-expect_list">expect_list</a>(). Thus <a href="#pxssh-expect">expect</a>() is<br> nothing more than::<br> <br> cpl = <a href="#pxssh-compile_pattern_list">compile_pattern_list</a>(pl)<br> return <a href="#pxssh-expect_list">expect_list</a>(cpl, timeout)<br> <br> If you are using <a href="#pxssh-expect">expect</a>() within a loop it may be more<br> efficient to compile the patterns first and then call <a href="#pxssh-expect_list">expect_list</a>().<br> This avoid calls in a loop to <a href="#pxssh-compile_pattern_list">compile_pattern_list</a>()::<br> <br> cpl = <a href="#pxssh-compile_pattern_list">compile_pattern_list</a>(my_pattern)<br> while some_condition:<br> ...<br> i = <a href="#pxssh-expect_list">expect_list</a>(clp, timeout)<br> ...</tt></dd></dl> <dl><dt><a name="pxssh-eof"><strong>eof</strong></a>(self)</dt><dd><tt>This returns True if the EOF exception was ever raised.</tt></dd></dl> <dl><dt><a name="pxssh-expect"><strong>expect</strong></a>(self, pattern, timeout<font color="#909090">=-1</font>, searchwindowsize<font color="#909090">=None</font>)</dt><dd><tt>This seeks through the stream until a pattern is matched. The<br> pattern is overloaded and may take several types. The pattern can be a<br> StringType, EOF, a compiled re, or a list of any of those types.<br> Strings will be compiled to re types. This returns the index into the<br> pattern list. If the pattern was not a list this returns index 0 on a<br> successful match. This may raise exceptions for EOF or TIMEOUT. To<br> avoid the EOF or TIMEOUT exceptions add EOF or TIMEOUT to the pattern<br> list. That will cause expect to match an EOF or TIMEOUT condition<br> instead of raising an exception.<br> <br> If you pass a list of patterns and more than one matches, the first match<br> in the stream is chosen. If more than one pattern matches at that point,<br> the leftmost in the pattern list is chosen. For example::<br> <br> # the input is 'foobar'<br> index = p.expect (['bar', 'foo', 'foobar'])<br> # returns 1 ('foo') even though 'foobar' is a "better" match<br> <br> Please note, however, that buffering can affect this behavior, since<br> input arrives in unpredictable chunks. For example::<br> <br> # the input is 'foobar'<br> index = p.expect (['foobar', 'foo'])<br> # returns 0 ('foobar') if all input is available at once,<br> # but returs 1 ('foo') if parts of the final 'bar' arrive late<br> <br> After a match is found the instance attributes 'before', 'after' and<br> 'match' will be set. You can see all the data read before the match in<br> 'before'. You can see the data that was matched in 'after'. The<br> re.MatchObject used in the re match will be in 'match'. If an error<br> occurred then 'before' will be set to all the data read so far and<br> 'after' and 'match' will be None.<br> <br> If timeout is -1 then timeout will be set to the self.<strong>timeout</strong> value.<br> <br> A list entry may be EOF or TIMEOUT instead of a string. This will<br> catch these exceptions and return the index of the list entry instead<br> of raising the exception. The attribute 'after' will be set to the<br> exception type. The attribute 'match' will be None. This allows you to<br> write code like this::<br> <br> index = p.expect (['good', 'bad', pexpect.EOF, pexpect.TIMEOUT])<br> if index == 0:<br> do_something()<br> elif index == 1:<br> do_something_else()<br> elif index == 2:<br> do_some_other_thing()<br> elif index == 3:<br> do_something_completely_different()<br> <br> instead of code like this::<br> <br> try:<br> index = p.expect (['good', 'bad'])<br> if index == 0:<br> do_something()<br> elif index == 1:<br> do_something_else()<br> except EOF:<br> do_some_other_thing()<br> except TIMEOUT:<br> do_something_completely_different()<br> <br> These two forms are equivalent. It all depends on what you want. You<br> can also just expect the EOF if you are waiting for all output of a<br> child to finish. For example::<br> <br> p = pexpect.<a href="pexpect.html#spawn">spawn</a>('/bin/ls')<br> p.expect (pexpect.EOF)<br> print p.before<br> <br> If you are trying to optimize for speed then see <a href="#pxssh-expect_list">expect_list</a>().</tt></dd></dl> <dl><dt><a name="pxssh-expect_exact"><strong>expect_exact</strong></a>(self, pattern_list, timeout<font color="#909090">=-1</font>, searchwindowsize<font color="#909090">=-1</font>)</dt><dd><tt>This is similar to <a href="#pxssh-expect">expect</a>(), but uses plain string matching instead<br> of compiled regular expressions in 'pattern_list'. The 'pattern_list'<br> may be a string; a list or other sequence of strings; or TIMEOUT and<br> EOF.<br> <br> This call might be faster than <a href="#pxssh-expect">expect</a>() for two reasons: string<br> searching is faster than RE matching and it is possible to limit the<br> search to just the end of the input buffer.<br> <br> This method is also useful when you don't want to have to worry about<br> escaping regular expression characters that you want to match.</tt></dd></dl> <dl><dt><a name="pxssh-expect_list"><strong>expect_list</strong></a>(self, pattern_list, timeout<font color="#909090">=-1</font>, searchwindowsize<font color="#909090">=-1</font>)</dt><dd><tt>This takes a list of compiled regular expressions and returns the<br> index into the pattern_list that matched the child output. The list may<br> also contain EOF or TIMEOUT (which are not compiled regular<br> expressions). This method is similar to the <a href="#pxssh-expect">expect</a>() method except that<br> <a href="#pxssh-expect_list">expect_list</a>() does not recompile the pattern list on every call. This<br> may help if you are trying to optimize for speed, otherwise just use<br> the <a href="#pxssh-expect">expect</a>() method. This is called by <a href="#pxssh-expect">expect</a>(). If timeout==-1 then<br> the self.<strong>timeout</strong> value is used. If searchwindowsize==-1 then the<br> self.<strong>searchwindowsize</strong> value is used.</tt></dd></dl> <dl><dt><a name="pxssh-expect_loop"><strong>expect_loop</strong></a>(self, searcher, timeout<font color="#909090">=-1</font>, searchwindowsize<font color="#909090">=-1</font>)</dt><dd><tt>This is the common loop used inside expect. The 'searcher' should be<br> an instance of searcher_re or searcher_string, which describes how and what<br> to search for in the input.<br> <br> See <a href="#pxssh-expect">expect</a>() for other arguments, return value and exceptions.</tt></dd></dl> <dl><dt><a name="pxssh-fileno"><strong>fileno</strong></a>(self)</dt><dd><tt>This returns the file descriptor of the pty for the child.</tt></dd></dl> <dl><dt><a name="pxssh-flush"><strong>flush</strong></a>(self)</dt><dd><tt>This does nothing. It is here to support the interface for a<br> File-like object.</tt></dd></dl> <dl><dt><a name="pxssh-getecho"><strong>getecho</strong></a>(self)</dt><dd><tt>This returns the terminal echo mode. This returns True if echo is<br> on or False if echo is off. Child applications that are expecting you<br> to enter a password often set ECHO False. See <a href="#pxssh-waitnoecho">waitnoecho</a>().</tt></dd></dl> <dl><dt><a name="pxssh-getwinsize"><strong>getwinsize</strong></a>(self)</dt><dd><tt>This returns the terminal window size of the child tty. The return<br> value is a tuple of (rows, cols).</tt></dd></dl> <dl><dt><a name="pxssh-interact"><strong>interact</strong></a>(self, escape_character<font color="#909090">='<font color="#c040c0">\x1d</font>'</font>, input_filter<font color="#909090">=None</font>, output_filter<font color="#909090">=None</font>)</dt><dd><tt>This gives control of the child process to the interactive user (the<br> human at the keyboard). Keystrokes are sent to the child process, and<br> the stdout and stderr output of the child process is printed. This<br> simply echos the child stdout and child stderr to the real stdout and<br> it echos the real stdin to the child stdin. When the user types the<br> escape_character this method will stop. The default for<br> escape_character is ^]. This should not be confused with ASCII 27 --<br> the ESC character. ASCII 29 was chosen for historical merit because<br> this is the character used by 'telnet' as the escape character. The<br> escape_character will not be sent to the child process.<br> <br> You may pass in optional input and output filter functions. These<br> functions should take a string and return a string. The output_filter<br> will be passed all the output from the child process. The input_filter<br> will be passed all the keyboard input from the user. The input_filter<br> is run BEFORE the check for the escape_character.<br> <br> Note that if you change the window size of the parent the SIGWINCH<br> signal will not be passed through to the child. If you want the child<br> window size to change when the parent's window size changes then do<br> something like the following example::<br> <br> import pexpect, struct, fcntl, termios, signal, sys<br> def sigwinch_passthrough (sig, data):<br> s = struct.pack("HHHH", 0, 0, 0, 0)<br> a = struct.unpack('hhhh', fcntl.ioctl(sys.stdout.<a href="#pxssh-fileno">fileno</a>(), termios.TIOCGWINSZ , s))<br> global p<br> p.<a href="#pxssh-setwinsize">setwinsize</a>(a[0],a[1])<br> p = pexpect.<a href="pexpect.html#spawn">spawn</a>('/bin/bash') # Note this is global and used in sigwinch_passthrough.<br> signal.signal(signal.SIGWINCH, sigwinch_passthrough)<br> p.<a href="#pxssh-interact">interact</a>()</tt></dd></dl> <dl><dt><a name="pxssh-isalive"><strong>isalive</strong></a>(self)</dt><dd><tt>This tests if the child process is running or not. This is<br> non-blocking. If the child was terminated then this will read the<br> exitstatus or signalstatus of the child. This returns True if the child<br> process appears to be running or False if not. It can take literally<br> SECONDS for Solaris to return the right status.</tt></dd></dl> <dl><dt><a name="pxssh-isatty"><strong>isatty</strong></a>(self)</dt><dd><tt>This returns True if the file descriptor is open and connected to a<br> tty(-like) device, else False.</tt></dd></dl> <dl><dt><a name="pxssh-kill"><strong>kill</strong></a>(self, sig)</dt><dd><tt>This sends the given signal to the child application. In keeping<br> with UNIX tradition it has a misleading name. It does not necessarily<br> kill the child unless you send the right signal.</tt></dd></dl> <dl><dt><a name="pxssh-next"><strong>next</strong></a>(self)</dt><dd><tt>This is to support iterators over a file-like object.</tt></dd></dl> <dl><dt><a name="pxssh-read"><strong>read</strong></a>(self, size<font color="#909090">=-1</font>)</dt><dd><tt>This reads at most "size" bytes from the file (less if the read hits<br> EOF before obtaining size bytes). If the size argument is negative or<br> omitted, read all data until EOF is reached. The bytes are returned as<br> a string object. An empty string is returned when EOF is encountered<br> immediately.</tt></dd></dl> <dl><dt><a name="pxssh-read_nonblocking"><strong>read_nonblocking</strong></a>(self, size<font color="#909090">=1</font>, timeout<font color="#909090">=-1</font>)</dt><dd><tt>This reads at most size characters from the child application. It<br> includes a timeout. If the read does not complete within the timeout<br> period then a TIMEOUT exception is raised. If the end of file is read<br> then an EOF exception will be raised. If a log file was set using<br> <a href="#pxssh-setlog">setlog</a>() then all data will also be written to the log file.<br> <br> If timeout is None then the read may block indefinitely. If timeout is -1<br> then the self.<strong>timeout</strong> value is used. If timeout is 0 then the child is<br> polled and if there was no data immediately ready then this will raise<br> a TIMEOUT exception.<br> <br> The timeout refers only to the amount of time to read at least one<br> character. This is not effected by the 'size' parameter, so if you call<br> <a href="#pxssh-read_nonblocking">read_nonblocking</a>(size=100, timeout=30) and only one character is<br> available right away then one character will be returned immediately.<br> It will not wait for 30 seconds for another 99 characters to come in.<br> <br> This is a wrapper around os.<a href="#pxssh-read">read</a>(). It uses select.select() to<br> implement the timeout.</tt></dd></dl> <dl><dt><a name="pxssh-readline"><strong>readline</strong></a>(self, size<font color="#909090">=-1</font>)</dt><dd><tt>This reads and returns one entire line. A trailing newline is kept<br> in the string, but may be absent when a file ends with an incomplete<br> line. Note: This <a href="#pxssh-readline">readline</a>() looks for a \r\n pair even on UNIX<br> because this is what the pseudo tty device returns. So contrary to what<br> you may expect you will receive the newline as \r\n. An empty string<br> is returned when EOF is hit immediately. Currently, the size argument is<br> mostly ignored, so this behavior is not standard for a file-like<br> object. If size is 0 then an empty string is returned.</tt></dd></dl> <dl><dt><a name="pxssh-readlines"><strong>readlines</strong></a>(self, sizehint<font color="#909090">=-1</font>)</dt><dd><tt>This reads until EOF using <a href="#pxssh-readline">readline</a>() and returns a list containing<br> the lines thus read. The optional "sizehint" argument is ignored.</tt></dd></dl> <dl><dt><a name="pxssh-send"><strong>send</strong></a>(self, s)</dt><dd><tt>This sends a string to the child process. This returns the number of<br> bytes written. If a log file was set then the data is also written to<br> the log.</tt></dd></dl> <dl><dt><a name="pxssh-sendcontrol"><strong>sendcontrol</strong></a>(self, char)</dt><dd><tt>This sends a control character to the child such as Ctrl-C or<br> Ctrl-D. For example, to send a Ctrl-G (ASCII 7)::<br> <br> child.<a href="#pxssh-sendcontrol">sendcontrol</a>('g')<br> <br> See also, <a href="#pxssh-sendintr">sendintr</a>() and <a href="#pxssh-sendeof">sendeof</a>().</tt></dd></dl> <dl><dt><a name="pxssh-sendeof"><strong>sendeof</strong></a>(self)</dt><dd><tt>This sends an EOF to the child. This sends a character which causes<br> the pending parent output buffer to be sent to the waiting child<br> program without waiting for end-of-line. If it is the first character<br> of the line, the <a href="#pxssh-read">read</a>() in the user program returns 0, which signifies<br> end-of-file. This means to work as expected a <a href="#pxssh-sendeof">sendeof</a>() has to be<br> called at the beginning of a line. This method does not send a newline.<br> It is the responsibility of the caller to ensure the eof is sent at the<br> beginning of a line.</tt></dd></dl> <dl><dt><a name="pxssh-sendintr"><strong>sendintr</strong></a>(self)</dt><dd><tt>This sends a SIGINT to the child. It does not require<br> the SIGINT to be the first character on a line.</tt></dd></dl> <dl><dt><a name="pxssh-sendline"><strong>sendline</strong></a>(self, s<font color="#909090">=''</font>)</dt><dd><tt>This is like <a href="#pxssh-send">send</a>(), but it adds a line feed (os.linesep). This<br> returns the number of bytes written.</tt></dd></dl> <dl><dt><a name="pxssh-setecho"><strong>setecho</strong></a>(self, state)</dt><dd><tt>This sets the terminal echo mode on or off. Note that anything the<br> child sent before the echo will be lost, so you should be sure that<br> your input buffer is empty before you call <a href="#pxssh-setecho">setecho</a>(). For example, the<br> following will work as expected::<br> <br> p = pexpect.<a href="pexpect.html#spawn">spawn</a>('cat')<br> p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).<br> p.expect (['1234'])<br> p.expect (['1234'])<br> p.<a href="#pxssh-setecho">setecho</a>(False) # Turn off tty echo<br> p.sendline ('abcd') # We will set this only once (echoed by cat).<br> p.sendline ('wxyz') # We will set this only once (echoed by cat)<br> p.expect (['abcd'])<br> p.expect (['wxyz'])<br> <br> The following WILL NOT WORK because the lines sent before the setecho<br> will be lost::<br> <br> p = pexpect.<a href="pexpect.html#spawn">spawn</a>('cat')<br> p.sendline ('1234') # We will see this twice (once from tty echo and again from cat).<br> p.<a href="#pxssh-setecho">setecho</a>(False) # Turn off tty echo<br> p.sendline ('abcd') # We will set this only once (echoed by cat).<br> p.sendline ('wxyz') # We will set this only once (echoed by cat)<br> p.expect (['1234'])<br> p.expect (['1234'])<br> p.expect (['abcd'])<br> p.expect (['wxyz'])</tt></dd></dl> <dl><dt><a name="pxssh-setlog"><strong>setlog</strong></a>(self, fileobject)</dt><dd><tt>This method is no longer supported or allowed.</tt></dd></dl> <dl><dt><a name="pxssh-setmaxread"><strong>setmaxread</strong></a>(self, maxread)</dt><dd><tt>This method is no longer supported or allowed. I don't like getters<br> and setters without a good reason.</tt></dd></dl> <dl><dt><a name="pxssh-setwinsize"><strong>setwinsize</strong></a>(self, r, c)</dt><dd><tt>This sets the terminal window size of the child tty. This will cause<br> a SIGWINCH signal to be sent to the child. This does not change the<br> physical window size. It changes the size reported to TTY-aware<br> applications like vi or curses -- applications that respond to the<br> SIGWINCH signal.</tt></dd></dl> <dl><dt><a name="pxssh-terminate"><strong>terminate</strong></a>(self, force<font color="#909090">=False</font>)</dt><dd><tt>This forces a child process to terminate. It starts nicely with<br> SIGHUP and SIGINT. If "force" is True then moves onto SIGKILL. This<br> returns True if the child was terminated. This returns False if the<br> child could not be terminated.</tt></dd></dl> <dl><dt><a name="pxssh-wait"><strong>wait</strong></a>(self)</dt><dd><tt>This waits until the child exits. This is a blocking call. This will<br> not read any data from the child, so this will block forever if the<br> child has unread output and has terminated. In other words, the child<br> may have printed output then called exit(); but, technically, the child<br> is still alive until its output is read.</tt></dd></dl> <dl><dt><a name="pxssh-waitnoecho"><strong>waitnoecho</strong></a>(self, timeout<font color="#909090">=-1</font>)</dt><dd><tt>This waits until the terminal ECHO flag is set False. This returns<br> True if the echo mode is off. This returns False if the ECHO flag was<br> not set False before the timeout. This can be used to detect when the<br> child is waiting for a password. Usually a child application will turn<br> off echo mode when it is waiting for the user to enter a password. For<br> example, instead of expecting the "password:" prompt you can wait for<br> the child to set ECHO off::<br> <br> p = pexpect.<a href="pexpect.html#spawn">spawn</a> ('ssh user@example.com')<br> p.<a href="#pxssh-waitnoecho">waitnoecho</a>()<br> p.<a href="#pxssh-sendline">sendline</a>(mypassword)<br> <br> If timeout is None then this method to block forever until ECHO flag is<br> False.</tt></dd></dl> <dl><dt><a name="pxssh-write"><strong>write</strong></a>(self, s)</dt><dd><tt>This is similar to <a href="#pxssh-send">send</a>() except that there is no return value.</tt></dd></dl> <dl><dt><a name="pxssh-writelines"><strong>writelines</strong></a>(self, sequence)</dt><dd><tt>This calls <a href="#pxssh-write">write</a>() for each element in the sequence. The sequence<br> can be any iterable object producing strings, typically a list of<br> strings. This does not add line separators There is no return value.</tt></dd></dl> <hr> Data descriptors inherited from <a href="pexpect.html#spawn">pexpect.spawn</a>:<br> <dl><dt><strong>__dict__</strong></dt> <dd><tt>dictionary for instance variables (if defined)</tt></dd> </dl> <dl><dt><strong>__weakref__</strong></dt> <dd><tt>list of weak references to the object (if defined)</tt></dd> </dl> </td></tr></table></td></tr></table><p> <table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section"> <tr bgcolor="#55aa55"> <td colspan=3 valign=bottom> <br> <font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr> <tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td> <td width="100%"><strong>__all__</strong> = ['ExceptionPxssh', 'pxssh']<br> <strong>__revision__</strong> = '$Revision: 399 $'<br> <strong>__version__</strong> = '2.3'</td></tr></table> </body></html>
修改文件时间
将文件时间修改为当前时间的前一年
删除文件