Module of the Week - Pexpect

This week I discovered the Pexpect module. Where have you been all my life?!

What is Pexpect?

Pexpect is a cool automation module. Its main function is to automate interactive processes that provide predictable output. Think apps like telnet, ssh, git and so forth.
We automate these by telling pexpect what to expect from the application and then what to send back.

Pexpect essentially takes your place as a human and starts interacting with the application on your behalf!

The easiest way to explain this is to show you.

Key Functionality

There are two main uses for Pexpect that I’ll touch on in this article: spawn and run.

The Spawn Class

pexpect.spawn() is used to start your application off. The parameter you pass to it is the command you as a human overlord would type into your OS CLI. Eg:

child=pexpect.spawn(‘sshuser@192.168.1.1’)

Using this example, pexpect will kick off an ssh connection the host. We then have to tell pexpect what to expect back.

pexpect.expect(‘user@10.1.1.10\’spassword:’)

You can see that I’m literally telling pexpect what it should see when it initiates the first ssh command. I want pexpect to log into this box for me so I’ll need it to pass on my password (never hardcode your password into a script!):

pexpect.sendline(‘password’)

Pexpect has just submitted my password which results in the ssh connection being successful. I then go back and forth over and over until it’s accomplished the task.

I’ve put some quick sleeps in just to pause the script to allow time for the previous command to return.

Note the inclusion of the shell prompt # itself. This is actually necessary and indicates the return of the previous command.

Conclusion

While some of these use cases are definitely solvable using other functions and modules I’m still impressed by the possibilities of Pexpect. It’s quite flexible given I’m able to code in the return expectation.

I could have if statements to account for different kinds of return values (true or false etc). If the expect is different to what I’m… expecting (ha!), then I’d re-run the previous command or just exit with a fail. Lot’s of interesting things to try!

And yes, I know, a lot of this has already been solved by cron. I love cron so I won’t argue with you. I will say that not everything is an OS with cron. Start thinking home automation and suddenly Pexpect seems a little more usable!

Do you use Pexpect for anything cool? Let me know! I’m thinking this would be good fun to use with my Raspberry Pi!