www.noah.org/wiki/Pexpect#Q:_Why_not_just_use_a_pipe_.28popen.28.29.29.3F
pexpect current Description of Pexpect Pexpect is a pure Python expect-like module. Pexpect makes Python a better tool for controlling other applications. Pexpect is a pure Python module for spawning child applications; Pexpect allows your script to spawn a child application and control it as if a human were typing commands. Pexpect can be used for automating interactive applications such as ssh, ftp, passwd, telnet, etc. It can be used to a automate setup scripts for duplicating software package installations on different servers. Pexpect is in the spirit of Don Libes' Expect, but Pexpect is pure Python. Unlike other Expect-like modules for Python, Pexpect does not require TCL or Expect nor does it require C extensions to be compiled. It should work on any platform that supports the standard Python pty module.
License MIT style -- Free, open source, and all that good stuff. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
pexpect current The Pexpect tarball is a standard Python Distutil distribution. Running the following commands should get you a working Pexpect module. Note that you have to have root access to install a site package.
Requirements for use of Pexpect Python Pexpect was written and tested with Python 25 It should work on earlier versions that have the pty module. The pty module is part of the Standard Python Library, so if you are running on a POSIX system you should have it. I have taken effort to try to smooth the wrinkles out of the different platforms. Pexpect does not currently work on the standard Windows Python (see the pty requirement); It is possible to build something like a pty for Windows, but it would have to use a different technique that I am still investigating. I know it's possible because Libes' Expect was ported to Windows. If you have any ideas or skills to contribute in this area then I would really appreciate some tips on how to approach this problem.
SVN trunk Note that all PyUnit tests are under the tests/ directory. py Examples Under the distribution tarball directory you should find an "examples" directory. It calculates some simple statistical information on the number of external inet connections. This can be used to detect if one IP address is taking up an excessive number of connections. It can also send an email alert if a given IP address exceeds a threshold between runs of the script. This script can be used as a drop-in Munin plugin or it can be used stand-alone from cron. I used this on a busy web server that would sometimes get hit with denial of service attacks. This made it easy to see if a script was opening many multiple connections. A typical browser would open fewer than 10 connections at once. py This script creates SSH connections to a list of hosts that you provide. Each shell command that you enter is sent to all the hosts. For example, you could connect to a dozen different machines and reboot them all at once. py This implements a command similar to the classic BSD "script" command. This will start a subshell and log all input and output to a file. py This is for cleaning up binary files improperly added to CVS. checks with CVS to see if the sticky options are set to -kb; finally if sticky options are not -kb then uses 'cvs admin' to set the -kb option. and then gives the user interactive control over the session. In this case the "bookmark" is to a directory on the OpenBSD ftp server. py This runs a sequence of commands on a remote host using SSH. It runs a simple system checks such as uptime and free to monitor the state of the remote host. py This will login to each given server and change the password of the given user. py This starts the python interpreter and prints the greeting message backwards. It greatly simplifies the process of ripping a DVD to Divx (mpeg4) format. removing interlace artifacts, fitting to a target file size, etc. There are lots of options, but the process is simple and easy to use. It monitors the connection and restarts the tunnel if it goes down. py This will run the uptime command and parse the output into variables. This demonstrates using a single regular expression to match the output of a command and capturing different variable in match groups. The grouping regular expression handles a wide variety of different uptime formats.
Overview Pexpect can be used for automating interactive applications such as ssh, ftp, mencoder, passwd, etc. Here is an example of Pexpect in action: # This connects to the openbsd ftp site and # downloads the recursive directory listing. sendline ('bye') Obviously you could write an ftp client using Python's own ftplib module, but this is just a demonstration. This is especially handy if you are writing automated test tools. There are two important methods in Pexpect -- expect() and send() (or sendline() which is like send() with a linefeed). The expect() method waits for the child application to return a given strong. The string you specify is a regular expression, so you can match complicated patterns. The send() method writes a string to the child application. From the child's point of view it looks just like someone typed the text from a terminal. After each call to expect() the before and after properties will be set to the text printed by child application. The before property will contain all text up to the expected string pattern. The after string will contain the text that was matched by the expected pattern. An example of Pexpect in action may make things more clear. and then pass interactive control of the ftp session to the human user. Special EOF and TIMEOUT patterns There are two special patterns to match the End Of File or a Timeout condition. If the child has died and you have read all the child's output then ordinarily expect() will raise an EOF exception. In this case everything the child has output will be available in the before property. Lists if patterns The pattern given to expect() may be a regular expression or it may also be a list of regular expressions. The expect() method returns the index of the pattern that was matched. After entering a password you could get various responses from the server -- your password could be rejected; or you could be allowed in and asked for your terminal type; or you could be let right in and given a command prompt. sendline (my_secret_password) # We expect any of these three patterns...
after If nothing matches an expected pattern then expect will eventually raise a TIMEOUT exception. The default time is 30 seconds, but you can change this by passing a timeout argument to expect(): # Wait no more than 2 minutes (120 seconds) for password prompt. expect('password:', timeout=120) Find the end of line -- CR/LF conventions Matching at the end of a line can be tricky. Pexpect matches regular expressions a little differently than what you might be used to. The $ matches the end of string, but Pexpect reads from the child one character at a time, so each character looks like the end of a line. Pexpect can't do a look-ahead into the child's output stream. In general you would have this situation when using regular expressions with a stream. Note, pexpect does have an internal buffer, so reads are faster t...
|