18a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 28a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<html> 38a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<head> 48a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<title>Pexpect - Examples</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, Python, Libes, TCL, Expect, pipe, popen, pyExpect, expectpy, expect-like, expect-alike, expect like"> 108a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<meta name="Description" content="Examples for using Pexpect."> 118a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</head> 128a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<body bgcolor="#ffffff" text="#000000"> 138a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<div id="Header"> 148a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<h1>Pexpect Examples</h1> 158a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</div> 168a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<div id="Content"> 178a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 188a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><span class="code">hive.py</span></p> 198a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><blockquote> 208a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenThis script creates SSH connections to a list of hosts that 218a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenyou provide. Then you are given a command line prompt. Each 228a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenshell command that you enter is sent to all the hosts. The 238a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenresponse from each host is collected and printed. For example, 248a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenyou could connect to a dozen different machines and reboot 258a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthem all at once. 268a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p></blockquote> 278a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 288a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><span class="code">script.py</span></p> 298a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><blockquote> 308a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This implements a command similar to the classic BSD 318a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen"script" command. 328a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This will start a subshell and log all input and 338a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenoutput to a file. 348a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This demonstrates the interact() method of Pexpect. 358a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p></blockquote> 368a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 378a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><span class="code">fix_cvs_files.py</span></p> 388a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><blockquote> 398a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This is for cleaning up binary files improperly 408a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenadded to CVS. 418a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This script scans the given path to find binary 428a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenfiles; 438a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen checks with CVS to see if the sticky options are set 448a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chento -kb; 458a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen finally if sticky options are not -kb then uses 'cvs 468a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenadmin' to 478a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen set the -kb option. 488a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p></blockquote> 498a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 508a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><span class="code">ftp.py</span></p> 518a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><blockquote> 528a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This demonstrates an FTP "bookmark". 538a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This connects to an ftp site; does a few ftp stuff; 548a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenand then gives the user 558a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen interactive control over the session. In this case 568a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe "bookmark" is to a 578a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen directory on the OpenBSD ftp server. It puts you in 588a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe i386 packages 598a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen directory. You can easily modify this for other 608a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chensites. 618a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This demonstrates the interact() method of Pexpect. 628a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p></blockquote> 638a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 648a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><span class="code">monitor.py</span></p> 658a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><blockquote> 668a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This runs a sequence of commands on a remote host 678a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenusing SSH. 688a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen It runs a simple system checks such as uptime and 698a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenfree to monitor 708a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen the state of the remote host. 718a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p></blockquote> 728a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 738a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><span class="code">passmass.py</span></p> 748a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><blockquote> 758a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This will login to each given server and change the 768a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenpassword of the 778a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen given user. This demonstrates scripting logins and 788a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenpasswords. 798a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p></blockquote> 808a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 818a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><span class="code">python.py</span></p> 828a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><blockquote> 838a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This starts the python interpreter and prints the 848a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chengreeting message backwards. 858a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen It then gives the user iteractive control of Python. 868a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny ChenIt's pretty useless! 878a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p></blockquote> 888a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 898a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><span class="code">rippy.py</span></p> 908a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><blockquote> 918a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This is a wizard for mencoder. It greatly simplifies 928a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe process of 938a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen ripping a DVD to Divx (mpeg4) format. It can 948a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chentranscode from any 958a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen video file to another. It has options for resampling 968a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenthe audio stream; 978a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen removing interlace artifacts, fitting to a target 988a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenfile size, etc. 998a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen There are lots of options, but the process is simple 1008a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenand easy to use. 1018a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p></blockquote> 1028a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 1038a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><span class="code">sshls.py</span></p> 1048a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><blockquote> 1058a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This lists a directory on a remote machine. 1068a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p></blockquote> 1078a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><span class="code">ssh_tunnel.py</span></p> 1088a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><blockquote> 1098a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This starts an SSH tunnel to a remote machine. It 1108a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmonitors the connection 1118a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen and restarts the tunnel if it goes down. 1128a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p></blockquote> 1138a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><span class="code">uptime.py</span></p> 1148a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p><blockquote> 1158a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This will run the uptime command and parse the 1168a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenoutput into variables. 1178a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen This demonstrates using a single regular expression 1188a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chento match the output 1198a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen of a command and capturing different variable in 1208a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenmatch groups. 1218a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen The grouping regular expression handles a wide variety of different 1228a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chenuptime formats. 1238a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen </blockquote> 1248a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 1258a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<p> 1268a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen<a href="http://sourceforge.net/projects/pexpect/" 1278a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen title="The Pexpect project page on SourceForge.net"> <img 1288a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen src="http://sourceforge.net/sflogo.php?group_id=59762&type=5" 1298a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen alt="The Pexpect project page on SourceForge.net" border="0" 1308a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen height="31" width="105"> </a> 1318a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</p> 1328a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</div> 1338a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen 1348a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</body> 1358a3c0430323c28c1fbe8ceecd2cd8e58b64a9295Johnny Chen</html> 136