What did you Expect - a look at Python module Pexpect

On Tuesday I presented on Pexpect at the Grenoble Python User Group, a short presentation as Rene Ribaud was presenting DocOpt and staticjinja.

Pexpect is a pure-Python module implementing Expect functionality.  Expect is functionality to enable to automate interactions with some interactive process where the user is obliged to interact with the program.  Expect was originally written in tcl but versions have since appeared in many other languages.

When using expect you spawn a new process and then interact with that process through a series of interactions where the expect() is called and blocks until a suitable match occurs, e.g. a password prompt, and then send() is used to send text to the process as if sent by the user typing at the keyboard.

I started using pexpect because I had a special use case.  When installing a particular N-node OpenStack platform it was necessary to pass by several machines/accounts to reach the controller or compute nodes.  As machines and user accounts were automatically created with unknown passwords and installed ssh keys, it was necessary to perform a sequence of 3 ssh hops and 3 sudo calls to reach a compute or controller node.

Using pexpect it was possible to auto-login across those 6 accounts, by sending the password when necessary.  A script was written mhop_ssh.py to perform such multi-hop logins and even to install ssh keys along the way thus allowing traditional direct ssh connection to the controller or compute nodes.

I've placed my presentation notebook and the mhop_ssh.py script on github here.

For testing purposes I created a group of virtual servers using Docker, described here.


Popular Posts