OLD | NEW |
(Empty) | |
| 1 Examples |
| 2 ======== |
| 3 |
| 4 Under the distribution tarball directory you should find an "examples" directory
. |
| 5 This is the best way to learn to use Pexpect. See the descriptions of Pexpect |
| 6 Examples. |
| 7 |
| 8 `topip.py <https://github.com/pexpect/pexpect/blob/master/examples/topip.py>`_ |
| 9 This runs `netstat` on a local or remote server. It calculates some simple |
| 10 statistical information on the number of external inet connections. This can |
| 11 be used to detect if one IP address is taking up an excessive number of |
| 12 connections. It can also send an email alert if a given IP address exceeds a |
| 13 threshold between runs of the script. This script can be used as a drop-in |
| 14 Munin plugin or it can be used stand-alone from cron. I used this on a busy |
| 15 web server that would sometimes get hit with denial of service attacks. This |
| 16 made it easy to see if a script was opening many multiple connections. A |
| 17 typical browser would open fewer than 10 connections at once. A script might |
| 18 open over 100 simultaneous connections. |
| 19 |
| 20 `hive.py <https://github.com/pexpect/pexpect/blob/master/examples/hive.py>`_ |
| 21 This script creates SSH connections to a list of hosts that you provide. |
| 22 Then you are given a command line prompt. Each shell command that you |
| 23 enter is sent to all the hosts. The response from each host is collected |
| 24 and printed. For example, you could connect to a dozen different |
| 25 machines and reboot them all at once. |
| 26 |
| 27 `script.py <https://github.com/pexpect/pexpect/blob/master/examples/script.py>`_ |
| 28 This implements a command similar to the classic BSD "script" command. |
| 29 This will start a subshell and log all input and output to a file. |
| 30 This demonstrates the :meth:`~pexpect.spawn.interact` method of Pexpect. |
| 31 |
| 32 `ftp.py <https://github.com/pexpect/pexpect/blob/master/examples/ftp.py>`_ |
| 33 This demonstrates an FTP "bookmark". This connects to an ftp site; |
| 34 does a few ftp tasks; and then gives the user interactive control over |
| 35 the session. In this case the "bookmark" is to a directory on the |
| 36 OpenBSD ftp server. It puts you in the i386 packages directory. You |
| 37 can easily modify this for other sites. This demonstrates the |
| 38 :meth:`~pexpect.spawn.interact` method of Pexpect. |
| 39 |
| 40 `monitor.py <https://github.com/pexpect/pexpect/blob/master/examples/monitor.py>
`_ |
| 41 This runs a sequence of commands on a remote host using SSH. It runs a |
| 42 simple system checks such as uptime and free to monitor the state of |
| 43 the remote host. |
| 44 |
| 45 `passmass.py <https://github.com/pexpect/pexpect/blob/master/examples/passmass.p
y>`_ |
| 46 This will login to each given server and change the password of the |
| 47 given user. This demonstrates scripting logins and passwords. |
| 48 |
| 49 `python.py <https://github.com/pexpect/pexpect/blob/master/examples/python.py>`_ |
| 50 This starts the python interpreter and prints the greeting message |
| 51 backwards. It then gives the user iteractive control of Python. It's |
| 52 pretty useless! |
| 53 |
| 54 `ssh_tunnel.py <https://github.com/pexpect/pexpect/blob/master/examples/ssh_tunn
el.py>`_ |
| 55 This starts an SSH tunnel to a remote machine. It monitors the |
| 56 connection and restarts the tunnel if it goes down. |
| 57 |
| 58 `uptime.py <https://github.com/pexpect/pexpect/blob/master/examples/uptime.py>`_ |
| 59 This will run the uptime command and parse the output into variables. |
| 60 This demonstrates using a single regular expression to match the |
| 61 output of a command and capturing different variable in match groups. |
| 62 The grouping regular expression handles a wide variety of different |
| 63 uptime formats. |
OLD | NEW |