Automating Interactive Processes with Expect on macOS

By Jeff Vincent|

May 11, 2022

Expect is a Unix based utility that is a common means of automating processes that require user input. Scripts written with expect work by “expecting” a specific prompt or response from the interactive process being called, and passing the appropriate response when prompted. This could be user credentials, specific keystrokes or anything a user might be prompted for in an interactive CLI.

These scripts can be written manually, but today, we’ll look at a simple, automated way of creating the expect scripts themselves. To do so, we’ll need to install autoexpect, which we can achieve on macOS by running the following:

brew install expect 

Then, we’ll need to wrap our targeted process in a bash script that we can pass as an argument to autoexpect, like so:

autoexpect /path/to/our/script.sh 

This command tells expect to call the process you’d like to automate, and then walks you through the actual interactive prompts associated with the script.sh process. It then produces an expect script called script.exp that – when called – will call your targeted process once more, but this time it will enter the same input you passed manually, but in an automated fashion.

You can then execute the original process defined in your script.sh file by calling ./script.exp.

NOTE: the filepath to our script.sh is hardcoded into the script.exp, so it’s important to put it where you’d like it to live in the filesystem before passing the filepath as an argument to autoexpect, as shown above.

Automating Homebrew installation

To demonstrate this process, let’s create a pair of scripts that install Homebrew without requiring an interactive user session. First, we’ll create a script called install_brew.sh, which will need to contain the following:

# install_brew.sh
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then, with autoexpect installed, we can create our autogenerated expect script by running the following:

autoexpect /path/to/install_brew.sh

When we do, we’ll be prompted as we normally would when installing Homebrew, which will look like the following:

admin@admins-iMac-Pro ~ % autoexpect /Users/admin/install_brew.sh
autoexpect started, file is script.exp
==> Checking for `sudo` access (which may request your password)...
Password:
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
/usr/local/Homebrew

Press RETURN/ENTER to continue or any other key to abort:
==> /usr/bin/sudo /usr/sbin/chown -R admin:admin /usr/local/Homebrew
==> Downloading and installing Homebrew...

Once this process is completed, it will produce a script in the local directory called script.exp.

Then, we can test the autogenerated script by running ./script.exp. If all went well, we should see the same output as is shown above, but our new expect script will submit the same input we provided when we created it automagically.

TL;DR

Expect is a powerful, Unix-based utility that allows users to automate interactions with interactive command line interfaces (CLIs). They can be very handy for automating the provisioning of new machines remotely. For example, above we looked at the example of automating the installation of Homebrew, which would normally require that a user manually remoted into a server. However, with our nifty expect script, we can achieve the same result in a fully automated way.

Share this article

Logo

Orka, Orka Workspace and Orka Pulse are trademarks of MacStadium, Inc. Apple, Mac, Mac mini, Mac Pro, Mac Studio, and macOS are trademarks of Apple Inc. The names and logos of third-party products and companies shown on the website are the property of their respective owners and may also be trademarked.

©2024 MacStadium, Inc. is a U.S. corporation headquartered at 3525 Piedmont Road, NE, Building 7, Suite 700, Atlanta, GA 30305. MacStadium, Ltd. is registered in Ireland, company no. 562354.