The White Rabbit

It is better to be approximately right than precisely wrong. (Warren Buffet)

Published on Saturday, 27 April 2024

Tags: bash2 automation1 expect1

Automating Interactive Bash Scripts with Expect

Learn how to automate interactive Bash scripts using Expect, an essential tool for scripting automated interactions with programs that require user input.


When working with Bash scripts that interact with other systems or services, automating the login process or any form of user interaction can streamline your workflows significantly. One powerful tool for this purpose is Expect, a program that automates interactions with applications that use a text-based interface.

What is Expect?

Expect is a scripting language designed for automating terminal applications. It is built on top of Tcl (Tool Command Language) and works by expecting specific text outputs from a program and responding to them as a human would.

How does it work?

Expect is a tool that automates interactions with programs that require user input. It watches for specific text outputs (like prompts) and automatically responds, mimicking what a human would type. This helps in automating tasks like logging into servers or interacting with scripts.

Example: Automating SSH Logins

Here's a practical example where Expect is used to automate an SSH login process that requires entering a passphrase for authentication.

#!/usr/bin/expect -f

# Define variables in a .pw file
# Syntax: set varName "varValue"
source /export/common/.youruser/some_vars.pw

# Run the interactive bash script
spawn "./bashScriptDoingSSHLogin.sh"

# Wait for passphrase prompt
expect "Enter passphrase for key '$varname1':"

# Provide the passphrase
send -- "$varname2\r"

# Wait for the sftp prompt
expect "sftp>"

# Exit sftp session
send -- "bye\r"

# End expect script
expect eof

Breaking Down the Script

  1. Starting Expect: The script begins with #!/usr/bin/expect -f, indicating that this script should be run with Expect.

  2. Source Variables: It sources variables from a file, making them available for use in the script.

  3. Spawning a Process: The spawn command starts the process you want to interact with—in this case, a Bash script handling SSH login.

  4. Handling Prompts: expect waits for specific text (a passphrase prompt) before sending the required passphrase using send.

  5. Ending the Session: After completing the necessary interactions, the script sends a command to end the session and waits for the end of the file (eof), which signifies that the expect script has finished executing.

Benefits of Using Expect

  • Automation of repetitive tasks: Reduces the need for manual input in scripts.
  • Error handling: Can be programmed to handle unexpected prompts or errors.
  • Enhanced script reliability: Makes scripts more robust and user-independent.

Expect is invaluable for sysadmins, testers, and developers who frequently manage interactive processes within scripts. By mastering Expect, you can take your scripting capabilities to a new level, saving time and reducing potential errors.