18a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 28a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<html> 38a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<head> 48a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<title>Pexpect - a Pure Python Expect-like module</title> 58a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<link rel="stylesheet" href="clean.css" type="text/css"> 68a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 78a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<meta name="Author" content="Noah Spurrier"> 88a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<meta name="Keywords" 98a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen content="pexpect, Noah Spurrier, pypect, Python, Libes, TCL, Expect, pipe, popen, pyExpect, expectpy, expect-like, expect-alike, expect like"> 108a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<meta name="Description" 118a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen content="Pexpect is a pure Python Expect-like module. Pexpect makes Python a better tool for controlling other applications."> 128a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</head> 138a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<body bgcolor="#ffffff" text="#000000"> 148a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<div id="Header"> 158a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1>Pexpect version 2.4<br> 168a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chena Pure Python Expect-like module 178a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</h1> 188a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</div> 198a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<div id="Content"> 208a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Pexpect makes Python a better tool for controlling other 218a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenapplications.</p> 228a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Pexpect is a pure Python module for spawning child applications; 238a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencontrolling them; and responding to expected patterns in their output. 248a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPexpect works like Don Libes' Expect. Pexpect allows your script to 258a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenspawn a child application and control it as if a human were typing 268a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencommands.</p> 278a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Pexpect can be used for automating interactive applications such as 288a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenssh, ftp, passwd, telnet, etc. It can be used to a automate setup 298a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenscripts for duplicating software package installations on different 308a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenservers. It can be used for automated software testing. Pexpect is in 318a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe spirit of Don Libes' Expect, but Pexpect is pure Python. Unlike 328a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenother Expect-like modules for Python, Pexpect does not require TCL or 338a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenExpect nor does it require C extensions to be compiled. It should work 348a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenon any platform that supports the standard Python pty module. The 358a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPexpect interface was designed to be easy to use.</p> 368a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<table border="0"> 378a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tbody> 388a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 398a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td align="right" valign="top">Send questions to:</td> 408a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td align="left"><a href="http://www.noah.org/email/"><img 418a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen src="email.png" alt="Click to send email." border="0" height="16" 428a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen width="100"></a></td> 438a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 448a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tbody> 458a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</table> 468a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> 478a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1><a name="license"></a>License: MIT style</h1> 488a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> 498a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenFree, open source, and all that good stuff.<br> 508a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<br> 518a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPermission is hereby granted, free of charge, to any person obtaining a copy 528a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenof this software and associated documentation files (the "Software"), to deal 538a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenin the Software without restriction, including without limitation the rights 548a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chento use, copy, modify, merge, publish, distribute, sublicense, and/or sell 558a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencopies of the Software, and to permit persons to whom the Software is 568a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenfurnished to do so, subject to the following conditions:<br> 578a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<br> 588a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThe above copyright notice and this permission notice shall be included in all 598a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencopies or substantial portions of the Software.<br> 608a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<br> 618a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 628a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 638a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 648a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 658a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenDAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 668a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenOTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 678a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenUSE OR OTHER DEALINGS IN THE SOFTWARE.<br> 688a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<br> 698a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPexpect Copyright (c) 2008 Noah Spurrier<br> 708a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenhttp://pexpect.sourceforge.net/ 718a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 728a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 738a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> 748a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1><a name="download"></a><a 758a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen href="http://sourceforge.net/project/showfiles.php?group_id=59762">Download</a></h1> 768a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Download the <a 778a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen href="http://sourceforge.net/project/showfiles.php?group_id=59762"> 788a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencurrent version here</a> from the SourceForge site. Grab the current Pexpect tarball. 798a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 808a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Installing Pexpect</h2> 818a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>The Pexpect tarball is a standard Python Distutil distribution.</p> 828a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<ol> 838a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <li>download <span class="code">pexpect-2.4.tar.gz</span></li> 848a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <li><span class="code">tar zxf pexpect-2.4.tar.gz</span></li> 858a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <li><span class="code">cd pexpect-2.4</span></li> 868a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <li><span class="code">python setup.py install</span> <i>do this as root</i></li> 878a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</ol> 888a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Examples</h2> 898a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> 908a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenUnder the <span class="code">pexpect-2.4</span> directory you should find 918a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe <span class="code">examples</span> directory. 928a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThis is the best way to learn to use Pexpect. 938a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenSee the descriptions of <a href="examples.html">Pexpect Examples</a>. 948a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 958a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2><a name="doc"></a>API Documentation</h2> 968a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> 978a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 988a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="pexpect.html">pexpect</a> This is the main module that you want.<br> 998a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="pxssh.html">pxssh</a> Pexpect SSH is an extension of 'pexpect.spawn' that specializes in SSH.<br> 1008a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 1018a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe following are experimental extensions to Pexpect<br> 1028a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 1038a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="fdpexpect.html">fdpexpect</a> fdpexpect extension of 'pexpect.spawn' that uses an open file descriptor.<br> 1048a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="screen.html">SCREEN</a> This represents a virtual 'screen'.<br> 1058a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="ANSI.html">ANSI</a> This parses ANSI/VT-100 terminal escape codes.<br> 1068a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="FSM.html">FSM</a> This is a finite state machine used by ANSI.<br> 1078a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 1088a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 1098a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> 1108a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1><a name="status"></a>Project Status</h1> 1118a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Automated pyunit tests reach over 80% 1128a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencode coverage on pexpect.py. I regularly test on Linux and BSD 1138a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenplatforms. I try to test on Solaris and Irix. 1148a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 1158a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> 1168a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1><a name="requirements"></a>Requirements for use of Pexpect</h1> 1178a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Python</h2> 1188a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 1198a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p>Pexpect was written and tested with Python 2.4. It should work on 1208a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenearlier versions that have the <span class="code">pty</span> module. I 1218a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensometimes even manually test it with Python 1.5.2, but I can't easily 1228a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenrun the PyUnit test framework against Python 1.5.2, so I have less 1238a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenconfidence in Pexpect on Python 1.5.2.</p> 1248a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 1258a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>pty module</h2> 1268a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 1278a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p>Any POSIX system (UNIX) with a working <span class="code">pty</span> 1288a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmodule should be able to run Pexpect. The <span class="code">pty</span> 1298a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmodule is part of the Standard Python Library, so if you are running on 1308a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chena POSIX system you should have it. The <span class="code">pty</span> 1318a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmodule does not run the same on all platforms. It should be solid on Linux 1328a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenand BSD systems. I have taken effort to try to smooth the wrinkles out of the different platforms. To learn more 1338a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenabout the wrinkles see <a href="#bugs">Bugs</a> and <a href="#testing">Testing</a>.</p> 1348a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 1358a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Pexpect does not currently work on the standard Windows Python (see 1368a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe pty requirement); however, it seems to work fine using <a 1378a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen href="http://www.cygwin.com/">Cygwin</a>. It is possible to build 1388a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensomething like a pty for Windows, but it would have to use a different 1398a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chentechnique that I am still investigating. I know it's possible because 1408a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenLibes' Expect was ported to Windows. <i>If you have any ideas or 1418a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenskills to contribute in this area then I would really appreciate some 1428a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chentips on how to approach this problem.</i> </p> 1438a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> 1448a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1><a name="overview"></a>Overview</h1> 1458a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Pexpect can be used for automating interactive applications such as 1468a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenssh, ftp, mencoder, passwd, etc. The Pexpect interface was designed to be 1478a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Cheneasy to use. Here is an example of Pexpect in action:</p> 1488a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 1498a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <pre class="code"># This connects to the openbsd ftp site and<br># downloads the recursive directory listing.<br>import pexpect<br>child = pexpect.spawn ('ftp ftp.openbsd.org')<br>child.expect ('Name .*: ')<br>child.sendline ('anonymous')<br>child.expect ('Password:')<br>child.sendline ('noah@example.com')<br>child.expect ('ftp> ')<br>child.sendline ('cd pub')<br>child.expect('ftp> ')<br>child.sendline ('get ls-lR.gz')<br>child.expect('ftp> ')<br>child.sendline ('bye')<br></pre> 1508a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 1518a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> Obviously you could write an ftp client using Python's own <span 1528a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">ftplib</span> module, but this is just a demonstration. 1538a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenYou can use this technique with any application. This is especially 1548a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenhandy if you are writing automated test tools.</p> 1558a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 1568a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>There are two important methods in Pexpect -- <span class="code"><b>expect()</b></span> 1578a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenand <span class="code"><b>send()</b></span> (or <span class="code">sendline()</span> 1588a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwhich is like <span class="code">send()</span> with a linefeed). 1598a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThe <span class="code">expect()</span> method waits for the child application 1608a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chento return a given string. The string you specify is a regular expression, so 1618a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenyou can match complicated patterns. The <span class="code"><b>send()</b></span> method 1628a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwrites a string to the child application. From the child's point of 1638a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenview it looks just like someone typed the text from a terminal. After 1648a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Cheneach call to <span class="code"><b>expect()</b></span> the <span 1658a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code"><b>before</b></span> and <span class="code"><b>after</b></span> 1668a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenproperties will be set to the text printed by child application. The <span 1678a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code"><b>before</b></span> property will contain all text up to 1688a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe expected string pattern. The <span class="code"><b>after</b></span> string 1698a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwill contain the text that was matched by the expected pattern. 1708a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThe <span class="code">match</span> property is set to the <span class="code">re MatchObject</span>. 1718a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 1728a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 1738a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>An example of Pexpect in action may make things more clear. This example uses 1748a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<span class="code">ftp</span> to login to the OpenBSD site; list files 1758a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenin a directory; and then pass interactive control of the ftp session to 1768a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe human user.</p> 1778a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 1788a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <pre class="code">import pexpect<br>child = pexpect.spawn ('ftp ftp.openbsd.org')<br>child.expect ('Name .*: ')<br>child.sendline ('anonymous')<br>child.expect ('Password:')<br>child.sendline ('noah@example.com')<br>child.expect ('ftp> ')<br>child.sendline ('ls /pub/OpenBSD/')<br>child.expect ('ftp> ')<br>print child.before # Print the result of the ls command.<br>child.interact() # Give control of the child to the user.<br></pre> 1798a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 1808a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Special EOF and TIMEOUT patterns</h2> 1818a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> 1828a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThere are two special patterns to match the End Of File or a Timeout condition. 1838a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenYou you can pass these patterns to <span class="code">expect()</span>. 1848a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThese patterns are not regular expressions. Use them like predefined constants. 1858a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 1868a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>If the child has died and you have read all the child's output then ordinarily 1878a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<span class="code">expect()</span> will raise an <span class="code">EOF</span> 1888a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexception. You can read everything up to the EOF without generating an 1898a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexception by using the EOF pattern <span class="code">expect(pexpect.EOF)</span>. 1908a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenIn this case everything the child has output will be available in the <span 1918a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">before</span> property.</p> 1928a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>The pattern given to <span class="code">expect()</span> may be a 1938a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenregular expression or it may also be a <b>list</b> of regular expressions. 1948a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThis allows you to match multiple optional responses. The <span class="code">expect()</span> 1958a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmethod returns the index of the pattern that was matched. For example, 1968a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensay you wanted to login to a server. After entering a password you 1978a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencould get various responses from the server -- your password could be 1988a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenrejected; or you could be allowed in and asked for your terminal type; 1998a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenor you could be let right in and given a command prompt. The following 2008a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencode fragment gives an example of this:</p> 2018a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 2028a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <pre class="code">child.expect('password:')<br>child.sendline (my_secret_password)<br># We expect any of these three patterns...<br>i = child.expect (['Permission denied', 'Terminal type', '[#\$] '])<br>if i==0:<br> print 'Permission denied on host. Can't login'<br> child.kill(0)<br>elif i==2:<br> print 'Login OK... need to send terminal type.'<br> child.sendline('vt100')<br> child.expect ('[#\$] ')<br>elif i==3:<br> print 'Login OK.'<br> print 'Shell command prompt', child.after</pre> 2038a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 2048a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>If nothing matches an expected pattern then expect will eventually 2058a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenraise a TIMEOUT exception. The default time is 30 seconds, but you can 2068a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenchange this by passing a timeout argument to expect():</p> 2078a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 2088a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <pre class="code"># Wait no more than 2 minutes (120 seconds) for password prompt.<br>child.expect('password:', timeout=120)</pre> 2098a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 2108a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Find the end of line -- CR/LF conventions<br> 2118a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenMatching at the end of a line can be tricky<br> 2128a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen$ regex pattern is useless.<br> 2138a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</h2> 2148a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Pexpect matches regular expressions a little differently than what 2158a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenyou might be used to. 2168a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 2178a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><i><b>The $ pattern for end of line match is useless</b></i>. 2188a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThe $ matches the end of string, but Pexpect reads from the child 2198a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenone character at a time, so each character looks like the end of a line. 2208a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPexpect can't do a look-ahead into the child's output stream. 2218a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenIn general you would have this situation when using regular expressions 2228a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwith any stream.<br> 2238a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<i>Note, pexpect does have an internal buffer, so reads are faster 2248a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthan one character at a time, but from the user's perspective the regex 2258a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenpatterns test happens one character at a time.</i></p> 2268a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>The best way to match the end of a line is to look for the 2278a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chennewline: "\r\n" (CR/LF). Yes, that does appear to be DOS-style. 2288a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenIt may surprise some UNIX people to learn that terminal TTY device drivers 2298a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen(dumb, vt100, ANSI, xterm, etc.) all use the CR/LF combination to signify 2308a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe end of line. Pexpect uses a Pseudo-TTY device to talk to the child application, so 2318a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwhen the child app prints "\n" you actually see "\r\n". 2328a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 2338a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><b>UNIX uses just linefeeds to end lines of text, but not when it 2348a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencomes to TTY devices!</b> TTY devices are more like the Windows world. 2358a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenEach line of text end with a CR/LF combination. When you intercept data 2368a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenfrom a UNIX command from a TTY device you will find that the TTY device 2378a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenoutputs a CR/LF combination. A UNIX command may only write a linefeed 2388a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen(\n), but the TTY device driver converts it to CR/LF. This means that 2398a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenyour terminal will see lines end with CR/LF (hex <span class="code">0D 0A</span>). 2408a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenSince Pexpect emulates a terminal, to match ends of lines you have to 2418a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexpect the CR/LF combination.</p> 2428a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 2438a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p class="code">child.expect ('\r\n')</p> 2448a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 2458a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>If you just need to skip past a new line then <span class="code">expect 2468a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen('\n')</span> by itself will work, but if you are expecting a specific 2478a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenpattern before the end of line then you need to explicitly look for the 2488a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen\r. For example the following expects a word at the end of a line:</p> 2498a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 2508a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p class="code">child.expect ('\w+\r\n')</p> 2518a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 2528a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>But the following would both fail:</p> 2538a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 2548a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p class="code">child.expect ('\w+\n')</p> 2558a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 2568a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>And as explained before, trying to use '$' to match the end of line 2578a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwould not work either:</p> 2588a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 2598a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p class="code">child.expect ('\w+$')</p> 2608a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 2618a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>So if you need to explicitly look for the END OF LINE, you want to 2628a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenlook for the CR/LF combination -- not just the LF and not the $ pattern.</p> 2638a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>This problem is not limited to Pexpect. This problem happens any 2648a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chentime you try to perform a regular expression match on a stream. Regular 2658a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexpressions need to look ahead. With a stream it is hard to look ahead 2668a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbecause the process generating the stream may not be finished. There is no 2678a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenway to know if the process has paused momentarily or is finished and 2688a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwaiting for you. <font color="#cc0000">Pexpect must implicitly always 2698a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendo a NON greedy match (minimal) at the end of a input {### already said 2708a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthis}.</font> </p> 2718a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Pexpect compiles all regular expressions with the DOTALL flag. With 2728a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe DOTALL flag a "." will match a newline. See the Python <a 2738a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen href="http://www.python.org/doc/current/lib/node115.html#l2h-733">documentation</a></p> 2748a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Beware of + and * at the end of input.</h2> 2758a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Remember that any time you try to match a pattern that needs 2768a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenlook-ahead that you will always get a minimal match (non greedy). For 2778a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexample, the following will always return just one character:</p> 2788a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 2798a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p class="code">child.expect ('.+')</p> 2808a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 2818a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>This example will match successfully, but will always return no 2828a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencharacters:</p> 2838a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 2848a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p class="code">child.expect ('.*')</p> 2858a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 2868a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Generally any star * expression will match as little as possible</p> 2878a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>One thing you can do is to try to force a non-ambiguous character at 2888a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe end of your <span class="code">\d+</span> pattern. Expect that 2898a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencharacter to delimit the string. For example, you might try making the 2908a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenend of your pattrn be <span class="code">\D+</span> instead of <span 2918a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">\D*</span>. That means number digits alone would not 2928a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensatisfy the (<span class="code">\d+</span>) pattern. You would need 2938a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensome number(s) and at least one <span class="code">\D</span> at the 2948a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenend. </p> 2958a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Matching groups</h2> 2968a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>You can group regular expression using parenthesis. After a match, 2978a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe <span class="code">match</span> parameter of the spawn object will 2988a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencontain the Python Match object. </p> 2998a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Examples</h2> 3008a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Using "match" and groups...</p> 3018a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Debugging</h2> 3028a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>If you get the string value of a pexpect.spawn object you will get 3038a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenlots of useful debugging information. For debugging it's very useful to 3048a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenuse the following pattern:</p> 3058a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>try:<br> 3068a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen i = child.expect ([pattern1, pattern2, pattern3, 3078a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenetc])<br> 3088a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexcept:<br> 3098a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen print "Exception was thrown"<br> 3108a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen print "debug information:"<br> 3118a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen print str(child)<br> 3128a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 3138a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>It is also useful to log the child's input and out to a file or the 3148a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenscreen. The following will turn on logging and send output to stdout 3158a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen(the screen).<br> 3168a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 3178a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> child = pexpect.spawn (foo)<br> 3188a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen child.logfile = sys.stdout<br> 3198a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<br> 3208a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 3218a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> 3228a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1>Exceptions</h1> 3238a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><b>EOF</b></p> 3248a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Note that two flavors of EOF Exception may be thrown. They are 3258a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenvirtually identical except for the message string. For practical 3268a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenpurposes you should have no need to distinguish between them, but they 3278a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendo give a little extra information about what type of platform you are 3288a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenrunning. The two messages are:</p> 3298a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 3308a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p class="code">End Of File (EOF) in read(). Exception style platform.</p> 3318a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p class="code">End Of File (EOF) in read(). Empty string style 3328a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenplatform.</p> 3338a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 3348a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Some UNIX platforms will throw an exception when you try to read 3358a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenfrom a file descriptor in the EOF state. Other UNIX platforms instead 3368a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenquietly return an empty string to indicate that the EOF state has been 3378a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenreached.</p> 3388a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><b>Expecting EOF</b></p> 3398a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>If you wish to read up to the end of the child's output without 3408a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chengenerating an <span class="code">EOF</span> exception then use the <span 3418a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">expect(pexpect.EOF)</span> method.</p> 3428a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><b>TIMEOUT</b></p> 3438a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>The <span class="code">expect()</span> and <span class="code">read()</span> 3448a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmethods will also timeout if the child does not generate any output for 3458a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chena given amount of time. If this happens they will raise a <span 3468a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">TIMEOUT</span> exception. You can have these method 3478a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenignore a timeout and block indefinitely by passing None for the timeout 3488a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenparameter.</p> 3498a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 3508a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p class="code">child.expect(pexpect.EOF, timeout=None)</p> 3518a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 3528a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> 3538a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1><a name="faq"></a>FAQ</h1> 3548a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><b>Q: Why don't shell pipe and redirect (| and >) work when I 3558a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenspawn a command?</b></p> 3568a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> 3578a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 3588a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenA: Remember that Pexpect does NOT interpret shell meta characters such as 3598a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenredirect, pipe, or wild cards (>, |, or *). That's done by a shell not the 3608a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencommand you are spawning. This is a common mistake. If you want to run a 3618a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencommand and pipe it through another command then you must also start a shell. 3628a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenFor example: 3638a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 3648a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<pre> 3658a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen child = pexpect.spawn('/bin/bash -c "ls -l | grep LOG > log_list.txt"') 3668a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen child.expect(pexpect.EOF) 3678a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</pre> 3688a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 3698a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThe second form of spawn (where you pass a list of arguments) is useful in 3708a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensituations where you wish to spawn a command and pass it its own argument list. 3718a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThis can make syntax more clear. For example, the following is equivalent to 3728a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe previous example: 3738a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 3748a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<pre> 3758a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen shell_cmd = 'ls -l | grep LOG > log_list.txt' 3768a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen child = pexpect.spawn ('/bin/bash', ['-c', shell_cmd]) 3778a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen child.expect (pexpect.EOF) 3788a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</pre> 3798a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 3808a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 3818a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><b>Q: Isn't there already a Python Expect?</b></p> 3828a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>A: Yes, there are several of them. They usually require you to 3838a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencompile C. I wanted something that was pure Python and preferably a 3848a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensingle module that was simple to install. I also wanted something that 3858a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwas easy to use. This pure Python expect only recently became possible 3868a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwith the introduction of the pty module in the standard Python library. 3878a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPreviously C extensions were required.</p> 3888a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 3898a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><strong>Q: The before and after properties sound weird.</strong></p> 3908a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Originally I was going to model Pexpect more after Expect, but then 3918a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenI found that I could never remember how to get the context of the stuff 3928a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenI was trying to parse. I hate having to read my own documentation. I 3938a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendecided that it was easier for me to remember what before and after 3948a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwas. It just so happens that this is how the -B and -A options in grep 3958a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenworks, so that made it even easier for me to remember. Whatever makes 3968a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmy life easier is what's best.</p> 3978a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 3988a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><b>Q: Why not just use Expect?</b></p> 3998a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>A: I love it. It's great. I has bailed me out of some real jams, but 4008a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenI wanted something that would do 90% of what I need from Expect; be 10% 4018a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenof the size; and allow me to write my code in Python instead of TCL. 4028a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPexpect is not nearly as big as Expect, but Pexpect does everything I 4038a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenhave ever used Expect for. 4048a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<!-- :-P If I liked TCL then you wouldn't be reading this. My appologies to Don Libes -- Expect is cool, TK is cool, but TCL is only slightly better than Perl in my book. Hopefully after Expyct is done I will not need to use Expect anymore -- except for that lovely autoexpect tool. Damn, I wish I had that! --> </p> 4058a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 4068a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><b>Q: Why not just use a pipe (popen())?</b></p> 4078a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>A: A pipe works fine for getting the output to non-interactive 4088a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenprograms. If you just want to get the output from <span class="code">ls</span>, 4098a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<span class="code">uname</span>, or <span class="code">ping</span> 4108a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthen this works. Pipes do not work very well for interactive programs 4118a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenand pipes will almost certainly fail for most applications that ask for 4128a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenpasswords such as telnet, ftp, or ssh.</p> 4138a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>There are two reasons for this. </p> 4148a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>First an application may bypass stdout and print directly to its 4158a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencontrolling TTY. Something like SSH will do this when it asks you for a 4168a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenpassword. This is why you cannot redirect the password prompt because 4178a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenit does not go through stdout or stderr.</p> 4188a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>The second reason is because most applications are built using the C 4198a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenStandard IO Library (anything that uses <span class="code">#include 4208a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<stdio.h></span>). One of the features of the stdio library is 4218a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthat it buffers all input and output. Normally output is <b><i>line 4228a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbuffered</i></b> when a program is printing to a TTY (your terminal 4238a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenscreen). Every time the program prints a line-feed the currently 4248a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbuffered data will get printed to your screen. The problem comes when 4258a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenyou connect a pipe. The stdio library is smart and can tell that it is 4268a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenprinting to a pipe instead of a TTY. In that case it switches from line 4278a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbuffer mode to <i><b>block buffered</b></i>. In this mode the 4288a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencurrently buffered data is flushed when the buffer is full. This causes 4298a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmost interactive programs to deadlock. Block buffering is more 4308a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenefficient when writing to disks and pipes. Take the situation where a 4318a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenprogram prints a message "Enter your user name:\n" and then waits for 4328a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenyou type type something. In block buffered mode, the stdio library will 4338a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chennot put the message into the pipe even though a linefeed is printed. 4348a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThe result is that you never receive the message, yet the child 4358a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenapplication will sit and wait for you to type a response. Don't confuse 4368a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe stdio lib's buffer with the pipe's buffer. The pipe buffer is 4378a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenanother area that can cause problems. You could flush the input side of 4388a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chena pipe, whereas you have no control over the stdio library buffer. </p> 4398a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>More information: the Standard IO library has three states for a 4408a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenFILE *. These are: _IOFBF for block buffered; _IOLBF for line buffered; 4418a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenand _IONBF for unbuffered. The STDIO lib will use block buffering when 4428a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chentalking to a block file descriptor such as a pipe. This is usually not 4438a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenhelpful for interactive programs. Short of recompiling your program to 4448a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Cheninclude fflush() everywhere or recompiling a custom stdio library there 4458a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenis not much a controlling application can do about this if talking over 4468a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chena pipe.</p> 4478a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> The program may have put data in its output that remains unflushed 4488a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbecause the output buffer is not full; then the program will go and 4498a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendeadlock while waiting for input -- because you never send it any 4508a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbecause you are still waiting for its output (still stuck in the 4518a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenSTDIO's output buffer).</p> 4528a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>The answer is to use a pseudo-tty. A TTY device will force <i><b>line</b></i> 4538a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbuffering (as opposed to block buffering). Line buffering means that 4548a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenyou will get each line when the child program sends a line feed. This 4558a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencorresponds to the way most interactive programs operate -- send a line 4568a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenof output then wait for a line of input.</p> 4578a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I put "answer" in quotes because it's ugly solution and because 4588a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthere is no POSIX standard for pseudo-TTY devices (even though they 4598a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenhave a TTY standard...). What would make more sense to me would be to 4608a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenhave some way to set a mode on a file descriptor so that it will tell 4618a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe STDIO to be line-buffered. I have investigated, and I don't think 4628a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthere is a way to set the buffered state of a child process. The STDIO 4638a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenLibrary does not maintain any external state in the kernel or whatnot, 4648a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenso I don't think there is any way for you to alter it. I'm not quite 4658a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensure how this line-buffered/block-buffered state change happens 4668a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Cheninternally in the STDIO library. I think the STDIO lib looks at the 4678a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenfile descriptor and decides to change behavior based on whether it's a 4688a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenTTY or a block file (see isatty()).</p> 4698a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I hope that this qualifies as helpful.</p> 4708a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 4718a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1>Don't use a pipe to control another application...</h1> 4728a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Pexpect may seem similar to <span class="code">os.popen()</span> or 4738a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<span class="code">commands</span> module. The main difference is that 4748a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPexpect (like Expect) uses a pseudo-TTY to talk to the child 4758a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenapplication. Most applications do no work well through the system() 4768a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencall or through pipes. And probably all applications that ask a user to 4778a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chentype in a password will fail. These applications bypass the stdin and 4788a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenread directly from the TTY device. Many applications do not explicitly 4798a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenflush their output buffers. This causes deadlocks if you try to control 4808a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenan interactive application using a pipe. What happens is that most UNIX 4818a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenapplications use the stdio (#include <stdio.h>) for input and 4828a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenoutput. The stdio library behaves differently depending on where the 4838a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenoutput is going. There is no way to control this behavior from the 4848a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenclient end.<br> 4858a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 4868a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 4878a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><b>Q: Can I do screen scraping with this thing?</b></p> 4888a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>A: That depends. If your application just does line-oriented output 4898a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthen this is easy. If it does screen-oriented output then it may work, 4908a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbut it could be hard. For example, trying to scrape data from the 'top' 4918a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencommand would be hard. The top command repaints the text window. </p> 4928a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I am working on an ANSI / VT100 terminal emulator that will have 4938a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmethods to get characters from an arbitrary X,Y coordinate of the 4948a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenvirtual screen. It works and you can play with it, but I have no 4958a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenworking examples at this time.</p> 4968a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> 4978a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1><a name="bugs"></a>Bugs</h1> 4988a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Threads</h2> 4998a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>On Linux (RH 8) you cannot spawn a child from a different thread and 5008a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenpass the handle back to a worker thread. The child is successfully 5018a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenspawned but you can't interact with it. The only way to make it work is 5028a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chento spawn and interact with the child all in the same thread. [Adam 5038a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenKerrison] </p> 5048a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2><a name="echo_bug"></a>Timing issue with send() and sendline()</h2> 5058a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>This problem has been addressed and should not effect most users.</p> 5068a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>It is sometimes possible to read an echo of the string sent with <span 5078a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">send()</span> and <span class="code">sendline()</span>. 5088a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenIf you call <span class="code">sendline()</span> and then immediately 5098a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencall <span class="code">readline()</span> you may get part of your 5108a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenoutput echoed back. You may read back what you just wrote even if the 5118a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenchild application does not explicitly echo it. Timing is critical. This 5128a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencould be a security issue when talking to an application that asks for 5138a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chena password; otherwise, this does not seem like a big deal. <i>But why 5148a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendo TTYs do this</i>?</p> 5158a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>People usually report this when they are trying to control SSH or 5168a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensome other login. For example, if your code looks something like this: </p> 5178a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<pre class="code">child.expect ('[pP]assword:')<br>child.sendline (my_password)</pre> 5188a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><br> 5198a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 5208a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen1. SSH prints "password:" prompt to the user.<br> 5218a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen2. SSH turns off echo on the TTY device.<br> 5228a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen3. SSH waits for user to enter a password.<br> 5238a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 5248a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenWhen scripting with Pexpect what can happen is that Pexpect will response to the "password:" prompt 5258a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbefore SSH has had time to turn off TTY echo. In other words, Pexpect sends the password between 5268a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensteps 1. and 2., so the password gets echoed back to the TTY. I would call this an SSH bug. 5278a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 5288a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> 5298a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPexpect now automatically adds a short delay before sending data to a child process. 5308a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThis more closely mimics what happens in the usual human-to-app interaction. 5318a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThe delay can be tuned with the 'delaybeforesend' attribute of the spawn class. 5328a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenIn general, this fixes the problem for everyone and so this should not be an issue 5338a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenfor most users. For some applications you might with to turn it off. 5348a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen child = pexpect.spawn ("ssh user@example.com") 5358a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen child.delaybeforesend = 0 5368a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 5378a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><br> 5388a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 5398a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Try changing it to look like the following. I know that this fix 5408a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendoes not look correct, but it works. I have not figured out exactly 5418a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwhat is happening. You would think that the sleep should be after the 5428a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensendline(). The fact that the sleep helps when it's between the 5438a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexpect() and the sendline() must be a clue.</p> 5448a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<pre class="code">child.expect ('[pP]assword:')<br>child.sendline (my_password)</pre> 5458a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Timing issue with isalive()</h2> 5468a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Reading the state of isalive() immediately after a child exits may 5478a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensometimes return 1. This is a race condition. The child has closed its 5488a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenfile descriptor, but has not yet fully exited before Pexpect's 5498a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenisalive() executes. Addings a slight delay before the isalive() will 5508a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenhelp. In the following example <span class="code">isalive()</span> 5518a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensometimes returns 1:</p> 5528a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 5538a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <pre class="code">child = pexpect.spawn('ls')<br>child.expect(pexpect.EOF)<br>print child.isalive()</pre> 5548a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 5558a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>But if there is any delay before the call to <span class="code">isalive()</span> 5568a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthen it will always return 0 as expected.</p> 5578a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 5588a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <pre class="code">child = pexpect.spawn('ls')<br>child.expect(pexpect.EOF)<br>time.sleep(0.1)<br>print child.isalive()</pre> 5598a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 5608a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 5618a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Truncated output just before child exits</h2> 5628a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><i>So far I have seen this only on older versions of <b>Apple's MacOS X</b>.</i> 5638a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenIf the child application quits it may not flush its output buffer. This 5648a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmeans that your Pexpect application will receive an EOF even though it 5658a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenshould have received a little more data before the child died. This is 5668a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chennot generally a problem when talking to interactive child applications. 5678a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenOne example where it is a problem is when trying to read output from a 5688a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenprogram like '<span class="code">ls</span>'. You may receive most of 5698a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe directory listing, but the last few lines will get lost before you 5708a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenreceive an EOF. The reason for this is that '<span class="code">ls</span>' 5718a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenruns; completes its task; and then exits. The buffer is not flushed 5728a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbefore exit so the last few lines are lost. The following example 5738a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendemonstrates the problem:</p> 5748a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> </p> 5758a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 5768a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <pre class="code">child = pexpect.spawn ('ls -l')<br>child.expect (pexpect.EOF)<br>print child.before <br> </pre> 5778a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 5788a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p></p> 5798a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 5808a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Controlling SSH on Solaris</h2> 5818a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Pexpect does not yet work perfectly on Solaris. 5828a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenOne common problem is that SSH sometimes will not allow TTY password 5838a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenauthentication. For example, you may expect SSH to ask you for a 5848a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenpassword using code like this: 5858a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 5868a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<pre class="code">child = pexpect.spawn ('ssh user@example.com')<br>child.expect ('assword')<br>child.sendline ('mypassword')<br></pre> 5878a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenYou may see the following error come back from a spawned 5888a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenchild SSH: 5898a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p></p> 5908a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote>Permission denied (publickey,keyboard-interactive). </blockquote> 5918a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> 5928a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThis means that SSH thinks it can't access the TTY to ask you for your 5938a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenpassword. 5948a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThe only solution I have found is to use public key authentication with 5958a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenSSH. 5968a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThis bypasses the need for a password. I'm not happy with this 5978a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensolution. 5988a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThe problem is due to poor support for Solaris Pseudo TTYs in the 5998a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPython 6008a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenStandard Library. </p> 6018a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> 6028a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1><a name="changes"></a>CHANGES</h1> 6038a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>Current Release</h2> 6048a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Fixed OSError exception when a pexpect object is cleaned up. 6058a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPreviously you might have seen this exception:</p> 6068a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 6078a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <pre class="code">Exception exceptions.OSError: (10, 'No child processes') <br>in <bound method spawn.__del__ of<br><pexpect.spawn instance at 0xd248c>> ignored</pre> 6088a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 6098a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>You should not see that anymore. Thanks to Michael Surette.</p> 6108a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Added support for buffering reads. This greatly improves speed when 6118a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chentrying to match long output from a child process. When you create an 6128a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Cheninstance of the spawn object you can then set a buffer size. For now 6138a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenyou MUST do the following to turn on buffering -- it may be on by 6148a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendefault in future version.</p> 6158a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 6168a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <pre class="code">child = pexpect.spawn ('my_command')<br>child.maxread=1000 # Sets buffer to 1000 characters.</pre> 6178a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 6188a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<div> 6198a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I made a subtle change to the way TIMEOUT and EOF exceptions behave. 6208a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenPreviously you could either expect these states in which case pexpect 6218a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwill not raise an exception, or you could just let pexpect raise an 6228a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexception when these states were encountered. If you expected the 6238a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenstates then the 'before' property was set to everything before the 6248a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenstate was encountered, but if you let pexpect raise the exception then 6258a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen'before' was not set. Now the 'before' property will get set either way 6268a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenyou choose to handle these states.</p> 6278a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2><i>Older changes...</i></h2> 6288a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>The spawn object now provides iterators for a <i>file-like interface</i>. 6298a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThis makes Pexpect a more complete file-like object. You can now write 6308a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencode like this:</p> 6318a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 6328a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <pre class="code">child = pexpect.spawn ('ls -l')<br>for line in child:<br> print line<br></pre> 6338a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 6348a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I added the attribute <span class="code">exitstatus</span>. This 6358a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwill give the exit code returned by the child process. This will be set 6368a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chento <span class="code">None</span> while the child is still alive. When 6378a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<span class="code">isalive()</span> returns 0 then <span class="code">exitstatus</span> 6388a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwill be set.</p> 6398a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I made a few more tweaks to <span class="code">isalive()</span> so 6408a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthat it will operate more consistently on different platforms. Solaris 6418a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenis the most difficult to support.</p> 6428a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> </p> 6438a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>You can now put <span class="code">TIMEOUT</span> in a list of 6448a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexpected patterns. This is just like putting <span class="code">EOF</span> 6458a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenin the pattern list. Expecting for a <span class="code">TIMEOUT</span> 6468a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmay not be used as often as <span class="code">EOF</span>, but this 6478a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmakes Pexpect more consitent.</p> 6488a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Thanks to a suggestion and sample code from Chad J. Schroeder I 6498a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenadded the ability for Pexpect to operate on a file descriptor that is 6508a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenalready open. This means that Pexpect can be used to control streams 6518a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensuch as those from serial port devices. Now you just pass the integer 6528a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenfile descriptor as the "command" when contsructing a spawn open. For 6538a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexample on a Linux box with a modem on ttyS1:</p> 6548a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 6558a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <pre class="code">fd = os.open("/dev/ttyS1", os.O_RDWR|os.O_NONBLOCK|os.O_NOCTTY)<br>m = pexpect.spawn(fd) # Note integer fd is used instead of usual string.<br>m.send("+++") # Escape sequence<br>m.send("ATZ0\r") # Reset modem to profile 0<br>rval = m.expect(["OK", "ERROR"])</pre> 6568a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 6578a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h3>Pexpect now tests itself on Compile Farm!</h3> 6588a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I wrote a nice script that uses ssh to connect to each machine on 6598a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenSource Forge's Compile Farm and then run the testall.py script for each 6608a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenplatform. The result of the test is then recorded for each platform. 6618a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenNow it's easy to run regression tests across multiple platforms.</p> 6628a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h3>Pexpect is a file-like object</h3> 6638a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>The spawn object now provides a <i>file-like interface</i>. It 6648a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensupports most of the methods and attributes defined for Python File 6658a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenObjects. </p> 6668a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I changed write and writelines() so that they no longer return a 6678a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenvalue. Use send() if you need that functionality. I did this to make 6688a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe Spawn object more closely match a file-like object.</p> 6698a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>read() was renamed to read_nonblocking(). I added a new read() 6708a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmethod that matches file-like object interface. In general, you should 6718a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chennot notice the difference except that read() no longer allows you to 6728a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendirectly set the timeout value. I hope this will not effect any 6738a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexisting code. Switching to read_nonblocking() should fix existing code.</p> 6748a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I changed the name of <span class="code">set_echo()</span> to <span 6758a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">setecho()</span>.</p> 6768a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I changed the name of <span class="code">send_eof()</span> to <span 6778a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">sendeof()</span>.</p> 6788a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I modified <span class="code">kill()</span> so that it checks to 6798a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmake sure the pid isalive().</p> 6808a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>I modified <span class="code">spawn()</span> (really called from <span 6818a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">__spawn()</span>)so that it does not raise an expection 6828a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenif <span class="code">setwinsize()</span> fails. Some platforms such 6838a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenas Cygwin do not like setwinsize. This was a constant problem and since 6848a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenit is not a critical feature I decided to just silence the error. 6858a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenNormally I don't like to do that, but in this case I'm making an 6868a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenexception.</p> 6878a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Added a method <span class="code">close()</span> that does what you 6888a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthink. It closes the file descriptor of the child application. It makes 6898a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenno attempt to actually kill the child or wait for its status. </p> 6908a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Add variables <span class="code">__version__</span> and <span 6918a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">__revision__</span> (from cvs) to the pexpect modules. 6928a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThis is mainly helpful to me so that I can make sure that I'm testing 6938a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenwith the right version instead of one already installed.</p> 6948a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h3>Logging changes</h3> 6958a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 6968a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p><span class="code">log_open()</span> and <span class="code">log_close()</span> 6978a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenhave been removed. Now use <span class="code">setlog()</span>. The <span 6988a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">setlog()</span> method takes a file object. This is far 6998a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmore flexible than the previous log method. Each time data is written 7008a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chento the file object it will be flushed. To turn logging off simply call <span 7018a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">setlog()</span> with None.</p> 7028a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 7038a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h2>isalive changes</h2> 7048a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 7058a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p>I renamed the <span class="code">isAlive()</span> method to <span 7068a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">isalive()</span> to match the more typical naming style 7078a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenin Python. Also the technique used to detect child process status has 7088a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbeen drastically modified. Previously I did some funky stuff with 7098a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensignals which caused indigestion in other Python modules on some 7108a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenplatforms. It's was a big headache. It still is, but I think it works 7118a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbetter now.</p> 7128a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 7138a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h3>attribute name changes</h3> 7148a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 7158a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p>The names of some attributes have been changed. This effects the 7168a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chennames of the attributes that are set after called the <span 7178a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen class="code">expect()</span> method.</p> 7188a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <table class="pymenu" border="0" cellpadding="5"> 7198a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tbody> 7208a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 7218a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <th class="pymenu">NEW NAME</th> 7228a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <th class="pymenu">OLD NAME</th> 7238a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 7248a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 7258a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td><span class="code">before</span><br> 7268a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <i>Everything before the match.</i></td> 7278a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td><span class="code">before</span></td> 7288a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 7298a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 7308a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td><span class="code">after</span><br> 7318a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <i>Everything after and including the first character of the 7328a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmatch</i></td> 7338a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td><span class="code">matched</span></td> 7348a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 7358a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 7368a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td><span class="code">match</span><br> 7378a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <i>This is the re MatchObject from the match.<br> 7388a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenYou can get groups() from this.<br> 7398a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenSee '<span class="code">uptime.py</span>' in the examples tar ball.</i></td> 7408a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td><i>New -- Did not exist</i></td> 7418a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 7428a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tbody> 7438a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </table> 7448a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 7458a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h3>EOF changes</h3> 7468a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<blockquote> 7478a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p>The <span class="code">expect_eof()</span> method is gone. You 7488a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencan now simply use the <span class="code">expect()</span> method to 7498a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenlook for EOF.</p> 7508a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p>Was:</p> 7518a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <blockquote> 7528a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p><span class="code">p.expect_eof ()</span></p> 7538a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </blockquote> 7548a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p>Now:</p> 7558a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <blockquote> 7568a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p><span class="code">p.expect (pexpect.EOF)</span></p> 7578a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </blockquote> 7588a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</blockquote> 7598a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> 7608a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1><a name="testing"></a>TESTING</h1> 7618a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>The following platforms have been tested:</p> 7628a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<!-- 7638a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<table class="pymenu" border="0" cellpadding="5"> 7648a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tbody> 7658a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 7668a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <th class="pymenu">PLATFORM</th> 7678a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <th class="pymenu">RESULTS</th> 7688a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 7698a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 7708a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td>Linux 2.4.9-ac10-rmk2-np1-cerf2<br> 7718a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenarmv4l</td> 7728a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td><b><i>all tests passed</i></b></td> 7738a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 7748a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 7758a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td>Linux 2.4.18 #2<br> 7768a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensparc64</td> 7778a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td><b><i>all tests passed</i></b></td> 7788a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 7798a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 7808a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td>MacOS X Darwin Kernel Version 5.5<br> 7818a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenpowerpc</td> 7828a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td> 7838a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p>failed more than one test.</p> 7848a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p>Generally Pexpect works on OS X, but the nature of the quirks 7858a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chencause a many of the tests to fail. See <a href="#bugs">bugs</a> 7868a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen(Incomplete Child Output). The problem is more than minor, but Pexpect 7878a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenis still more than useful for most tasks. The problem is an edge case.</p> 7888a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </td> 7898a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 7908a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 7918a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td>Linux 2.2.20<br> 7928a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenalpha<br> 7938a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </td> 7948a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td><b><i>all tests passed</i></b></td> 7958a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 7968a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 7978a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td>Linux 2.4.18-5smp<br> 7988a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Cheni686</td> 7998a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td><b><i>all tests passed</i></b></td> 8008a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 8018a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 8028a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td>OpenBSD 2.9 GENERIC#653<br> 8038a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Cheni386</td> 8048a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td><b><i>all tests passed</i></b></td> 8058a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 8068a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 8078a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td>Solaris</td> 8088a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td> 8098a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p>failed <span class="code">test_destructor</span></p> 8108a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <p>Otherwise, this is working pretty well. The destructor problem 8118a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenis minor. For some reason, the <i>second</i> time a pty file 8128a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendescriptor is created and deleted it never gets returned for use. It 8138a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendoes not effect the first time or the third time or any time after 8148a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthat. It's only the second time. This is weird... This could be a file 8158a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chendescriptor leak, or it could be some peculiarity of how Solaris 8168a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenrecycles them. I thought it was a UNIX requirement for the OS to give 8178a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenyou the lowest available filedescriptor number. In any case, this 8188a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenshould not be a problem unless you create hundreds of pexpect 8198a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Cheninstances... It may also be a pty module bug. </p> 8208a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </td> 8218a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 8228a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 8238a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td>Windows XP Cygwin</td> 8248a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td>failed <span class="code">test_destructor</span>. That it 8258a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenworks at all is amazing to me. Cygwin rules!</td> 8268a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 8278a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tbody> 8288a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</table> 8298a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen--> 8308a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1> </h1> 8318a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1><a name="todo">TO DO</a></h1> 8328a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p>Add an option to add a delay after each expect() or before each 8338a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenread()/readline() call to automatically avoid the <a href="#echo_bug">echo 8348a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenbug</a>.</p> 8358a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> </p> 8368a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</div> 8378a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> 8388a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<table border="0"> 8398a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tbody> 8408a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <tr> 8418a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen <td> <a href="http://www.noah.org/email/"><img src="email.png" 8428a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen alt="Click to send email." border="0" height="16" width="100"></a> </td> 8438a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tr> 8448a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </tbody> 8458a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</table> 8468a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</div> 8478a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<div id="Menu"><b>INDEX</b><br> 8488a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<hr noshade="noshade" size="1"> <a href="#license" 8498a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen title="Python Software Foundation License">License</a><br> 8508a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="#download" title="Download and setup instructions">Download</a><br> 8518a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="#doc" title="Documentation and overview">Documentation</a><br> 8528a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="#status" title="Project Status">Project Status</a><br> 8538a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="#requirements" title="System requirements to use Pexpect">Requirements</a><br> 8548a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="#overview" title="Overview of what Pexpect does">Overview</a><br> 8558a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="#faq" title="FAQ">FAQ</a><br> 8568a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="#bugs" title="Bugs and work-arounds">Known Bugs</a><br> 8578a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="#changes" title="What's new with Pexpect">Recent Changes</a><br> 8588a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="#testing" title="Test results on various platforms">Testing</a><br> 8598a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="#todo" title="What to do next">To do</a><br> 8608a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="http://pexpect.svn.sourceforge.net/viewvc/pexpect/trunk/pexpect/" title="browse SVN">Browse SVN</a><br> 8618a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<br> 8628a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="http://sourceforge.net/projects/pexpect/" 8638a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen title="The Pexpect project page on SourceForge.net"> <img 8648a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen src="http://sourceforge.net/sflogo.php?group_id=59762&type=5" 8658a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen alt="The Pexpect project page on SourceForge.net" border="0" 8668a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen height="31" width="105"> </a> </div> 8678a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</body> 8688a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</html> 869