Automate interactive processes in Linux

There are circumstances where you have repetitive tasks you wish to automate. Most of that can be done through the use of scripts. But what do you do when you need to automate an interactive process?

In my case, I had to automate a configuration script. After installing a software tool on my servers, I always need to configure it, by entering some data through a console based wizard. Since I’m fully automating my server infrastructure, I needed an easy way to automate this process as well.

Expect & Autoexpect

In short, autoexpect watches you interacting with another program and creates an Expect script that reproduces your interactions. Basically, it registers all your interactions during a session.

To use (auto)expect, you’ll have to install packages:

$ sudo install expect expect-dev

The expect package installs the expect tool, so you can execute an expect script. The expect-dev package, installs the autoexpect tool, so you can record your tasks.

To start recording your session, start with the autoexpect command to generate a .exp file:

$ autoexpect -f register.exp /path/to/script/configure.sh

Exit the shell from the moment you are done “recording”, because by default, autoexpect spawns a new shell for you.

To re-run all recorded actions, issue the following command:

$ expect register.exp

Additionally, check the man pages for additional information and flags.