If I had a interactive program or shell like bash, how do I automate and script what I want to do with Python?

There’s a useful pip module called pexpect that you can install.

The idea of pexpect originated from a programming language called Expect that automates interactions with programs that expose a text terminal like ftp, ssh, etc.

Think of any program that opens a session that you have to exit!


  1. Python 2.7 or Python 3.3 or above
  2. pip install pexpect
  3. If you get lost, read the documentation.

Now, that you have installed pexpect, we can make a simple program as an example.


bash example. Sending one command.

Let’s say that I wanted to automate a new session of bash and use ls.

bash works a little differently than other interactive programs since it has a flag that allows input.

  1. You spawn the interactive program with pexpect.spawn("/bin/bash -c ls").
  2. You expect what comes after the command like \r\n (meaning new line) or the next command you know you’ll send.
  3. child.before is all the output before what we expect. We can print or use it in an if clause.


ssh example. Knowing what exactly we’re expecting. Sending multiple commands.

bash was a special example since the program allows input with its -c flag.

What if the program doesn’t have such a flag?

Let’s say that I wanted to automate a command on another machine with ssh.

Not just one command. Multiple commands.

  1. ssh into the machine.
  2. Create a file called slothparadise with touch.
  3. ls the current folder.


When we use pexpect to automate the manual process, we take a look at what we expect to happen.

We pinpoint what we will see after the command.

For ssh, it’s easy to see that after each command, we see ubuntu@ip-172-31-39-63:~.

We can use regex or in this case, it’s easy to use the word as it is.


regex example

You can expect uncertain output with regex.

If you don’t know what regex is, it is short for regular expression, which matches ranges of characters or words.

Let’s use regex with the ssh example.

Instead of expecting ubuntu@ip-172-31-39-63:~, we give room for expecting other users like root@ip-172-31-39-63:~.

Who knows? The user might be on root!

If you’re not familiar with regex, then I recommend to double-check your regex with this website.

The regex detects the highlighted blue.

  1. We create slothparadise2.
  2. We list the contents of the directory with ls

Instead of the word, I used the regex expression, and it works the same as before.

Those are the basics of pexpect! You can expand on these examples, and you’ll be on your way to scripting those tiresome interactive programs with an easy-to-use language like Python!