Expect is a tool for automating interactive programs: it “talks” to scripts or commands that prompt for input and sends responses without user interaction. You write small Expect scripts that spawn a process, wait for specific prompts, and send replies. Autoexpect records your interactive session and generates an Expect script for you. This guide shows how to install Expect, write a simple script, use variables and arguments, and use Autoexpect to generate scripts.
Related: How to set up an SFTP server on Debian 11 Server
Prerequisites
- A Linux system (Debian/Ubuntu or RHEL/CentOS/Rocky) with sudo.
- Basic familiarity with shell scripts.
Installation
Debian / Ubuntu (apt):
| |
RHEL / CentOS / Rocky (yum or dnf):
| |
Check that expect and autoexpect are on your PATH:
| |
Basic example: automating a simple prompt
Create a small bash script that asks for input. Save as name.sh:
| |
Make it executable and run it manually to see the prompt:
| |
It prints What is your name? and waits for input. To automate the response, use Expect.
Save the following as auto.exp:
| |
Make it executable and run it:
| |
Example output:
| |
The script spawns ./name.sh, expects the prompt string, sends John plus carriage return, then waits for end-of-file (eof).
Working with variables
Use set to define variables in Expect (similar to shell):
| |
Use them with $user, $age. To read command-line arguments, use lindex $argv:
| |
So USER is the first argument, PASSWORD the second (e.g. ./script.exp alice secret).
Example: SFTP password change script
You can automate an interactive SFTP session (e.g. changing password) by passing current password, new password, user, and server as arguments. Save as sftp_password_change.exp:
| |
Run it (replace with your values; avoid putting real passwords in shell history):
| |
Adjust the port 15422 and prompt strings if your SFTP server differs. For production, consider reading passwords from environment variables or a secure store instead of arguments.
Autoexpect: generate Expect scripts by recording
Autoexpect runs your interactive program, records what you type and what the program prints, and writes an Expect script. You then edit the generated file if needed.
Run your interactive script under autoexpect:
| |
When prompted, type the same answers you want to automate (e.g. John). When you exit the script, autoexpect writes script.exp.
Example session:
| |
The generated script.exp contains the Expect commands and the responses you typed. You can run it non-interactively:
| |
Autoexpect is useful for one-off installers or tools with many prompts: run once interactively, then reuse or schedule the generated script. Review and tidy the generated file (remove sensitive data, tighten timeouts or patterns) before using it in automation.
Example generated script (simplified)
The generated file includes comments and sometimes conservative mode. The core part looks like this:
| |
You can simplify patterns or add variables and arguments as needed.
Verifying
- Expect:
expect -c 'puts "ok"'should printok. - Basic script:
./auto.expshould runname.shand printYour name is Johnwithout prompting. - Autoexpect: After
autoexpect ./name.shand entering a value,./script.expshould reproduce the same run non-interactively.
Summary
Expect lets you automate interactive programs by spawning them and sending replies to prompts. You spawn a process, expect specific strings, and send responses; use set and $argv for variables and arguments. Autoexpect records an interactive session and generates an Expect script you can run unattended or schedule. Use Expect for SSH logins, SFTP, installers, or any tool that asks questions on the terminal; keep passwords and secrets out of scripts when possible (e.g. environment variables or a secrets manager).