Post

Linux expect Command

System administrators automate repetitive tasks all the time. Bash scripts provide many programs and features to carry out system automation tasks. The expect command offers a way to control interactive applications which require user input to continue.

Linux expect Command Syntax

The expect command runs Expect program scripts using the following syntax:

1
expect [options] [commands/command file]

The Except program uses the following keywords to interact with other programs:

CommandFunction
spawnCreates a new process.
sendSends a reply to the program.
expectWaits for output.
interactEnables interacting with the program.

Expect uses TCL (Tool Command Language) to control the program flow and essential interactions.

Some systems do not include Expect by default.

To install it with apt on Debian-based systems, run the following in the terminal:

1
sudo apt install expect

Alternatively, use yum on Red Hat-based systems:

1
yum install expect

Follow the installation instructions to complete the setup.

Linux expect Command Options

Below is a table describing available command options for the expect command:

CommandDescription
-cSpecifies the command to execute before the script.
-dProvides a brief diagnostic output.
-DInteractive debugger.
-fSpecifies a file to read from.
-iPrompts commands interactively.
-bReads file line by line (buffer).
-vPrint version.

Linux expect Command Examples

The next sections provide practical examples of the expect command, which executes Expect program scripts. To make an Expect script executable, add the following shebang at the start of each script:

1
#!/usr/bin/expect

The location differs depending on the system. To find the exact path, use:

1
whereis expect

Exchange the location if Expect is at a different location.

Autoexpect

Instead of writing Expect scripts from scratch, the Autoexpect program helps generate scripts interactively.

To demonstrate how Autoexpect works, do the following:

1. Run the Bash script from the first example (interactive_script.sh) using Autoexpect:

1
autoexpect ./interactive_script.sh

The output prints a confirmation message and the Expect script name (script.exp) to the console.

2. Provide answers to the questions. The replies save to the script.exp file and generate the Expect program code automatically. When complete, the output prints a confirmation.

3. Review the generated script in a text editor to see the code:

1
vim script.exp

The interactions are saved to the Expect script for future use.

Conclusion

After following the steps from this guide, you know the purpose of Expect scripts, the expect command, and some use cases.

Expect is a powerful automation tool for system administration tasks and code testing. Use the man command to review the complete manual for Expect.

Basic Expect Use

Below is a basic example that explains how the expect command functions:

1. Open a text editor and name the file interactive_script.sh. If you use Vim, run:

1
vim interactive_script.sh

2. Add the following code to the file:

1
2
3
4
5
6
7
8
#!/bin/bash

echo "Hello, who is this?"
read $REPLY
echo "What's your favorite color?"
read $REPLY
echo "How many cats do you have?"
read $REPLY

The code is a basic script with the read command that expects user interaction when run.

3. Save the file and close Vim:

1
:wq

4. Change the script to executable:

1
chmod +x interactive_script.sh

5. Create a new file to store the Expect script with:

1
vim expect_response.exp

The .exp extension is not mandatory, though it helps differentiate Expect scripts from other files.

6. Add the following code to the script:

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/expect

spawn ./interactive_script.sh
expect "Hello, who is this?\r"
send -- "phoenixNAP\r"
expect "What's your favorite color?\r"
send -- "Blue\r"
expect "How many cats do you have?\r"
send -- "1\r"
expect eof

The script consists of the following lines:

  • spawn creates a new process running the interactive_script.sh file.
  • expect writes the expected program message and waits for the output. The final line ends the program.
  • send contains the replies to the program after each expected message.

7. Save the file and close:

1
:wq

8. Make the script executable:

1
chmod +x expect_response.exp

9. Run the script with:

1
expect expect_response.exp

Or alternatively:

1
./expect_response.exp

The expect_response.exp script spawns the program process and sends automatic replies.

Examples

Create certificates with Easy-RSA for OpenVPN

When utilizing scripts that require data input during execution, such as Easy-RSA, which prompts you to enter data into multiple fields with different information, you can employ the following command:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
CMD="/etc/easy-rsa/easyrsa build-ca nopass"
expect << EOF
spawn $CMD
expect -exact "\r
Enter PEM pass phrase:"
send -- "<example>\r"
expect -exact "\r
Verifying - Enter PEM pass phrase:"
send -- "<example>\r"
expect -exact "\r
Common Name (eg: your user, host, or server name) \[Easy-RSA CA\]:"
send -- "<Name>\r"
expect eof
EOF
This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.