How to Automate Responses Using Expect and Autoexpect in Linux

Use Expect and Autoexpect to automate interactive prompts in Linux: install expect, write expect scripts, pass arguments, and record scripts with autoexpect.

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):

1
2
sudo apt update
sudo apt install -y expect

RHEL / CentOS / Rocky (yum or dnf):

1
2
3
4
5
# CentOS 7
sudo yum install -y expect

# RHEL 8, 9 and compatible (Rocky, Alma, etc.)
sudo dnf install -y expect

Check that expect and autoexpect are on your PATH:

1
2
which expect
which autoexpect

Basic example: automating a simple prompt

Create a small bash script that asks for input. Save as name.sh:

1
2
3
4
#!/bin/bash
echo "What is your name?"
read name
echo "Your name is ${name}"

Make it executable and run it manually to see the prompt:

1
2
chmod +x name.sh
./name.sh

It prints What is your name? and waits for input. To automate the response, use Expect.

Save the following as auto.exp:

1
2
3
4
5
6
#!/usr/bin/expect
set timeout -1
spawn ./name.sh
expect "What is your name?\r"
send -- "John\r"
expect eof

Make it executable and run it:

1
2
chmod +x auto.exp
./auto.exp

Example output:

1
2
3
4
5
$ ./auto.exp
spawn ./name.sh
What is your name?
John
Your name is John

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):

1
2
set user kip
set age 10

Use them with $user, $age. To read command-line arguments, use lindex $argv:

1
2
set USER [lindex $argv 0]
set PASSWORD [lindex $argv 1]

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/expect
set timeout 10
set curpass [lindex $argv 0]
set newpass [lindex $argv 1]
set user    [lindex $argv 2]
set server  [lindex $argv 3]

spawn sftp -P 15422 $user@$server
expect "Password:"
send "$curpass\r"
expect "Old Password:"
send "$curpass\r"
expect "New Password:"
send "$newpass\r"
expect "Reenter New Password:"
send "$newpass\r"
expect "sftp>"
send "exit\r"
expect eof

Run it (replace with your values; avoid putting real passwords in shell history):

1
2
chmod +x sftp_password_change.exp
./sftp_password_change.exp "$curpass" "$newpass" "$user" "$server"

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:

1
autoexpect ./name.sh

When prompted, type the same answers you want to automate (e.g. John). When you exit the script, autoexpect writes script.exp.

Example session:

1
2
3
4
5
6
$ autoexpect ./name.sh
autoexpect started, file is script.exp
What is your name?
John
Your name is John
autoexpect done, file is script.exp

The generated script.exp contains the Expect commands and the responses you typed. You can run it non-interactively:

1
./script.exp

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:

1
2
3
4
5
6
7
8
#!/usr/bin/expect -f
set timeout -1
spawn ./name.sh
match_max 100000
expect -exact "What is your name?\r
"
send -- "John\r"
expect eof

You can simplify patterns or add variables and arguments as needed.

Verifying

  • Expect: expect -c 'puts "ok"' should print ok.
  • Basic script: ./auto.exp should run name.sh and print Your name is John without prompting.
  • Autoexpect: After autoexpect ./name.sh and entering a value, ./script.exp should 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).

comments powered by Disqus
Citizix Ltd
Built with Hugo
Theme Stack designed by Jimmy