Post

Automating Linux Interactions with Expect

🛠️ Installing and Using expect in Linux

Introduction

expect is a command-line tool used to automate interactions with programs that require user input. It is particularly useful when dealing with interactive scripts or commands that require user intervention, such as SSH connections, FTP, and database systems. This guide will walk you through the process of installing expect on your Linux machine and demonstrate how to use it in different scenarios.

📥 Installing expect

To get started, we first need to install expect on your Linux system. The installation process varies depending on the distribution you are using.

For Ubuntu/Debian-based systems:

To install expect, open your terminal and run the following command:

1
2
sudo apt update
sudo apt install expect

For CentOS/RHEL-based systems

For CentOS or RHEL, use the following command to install expect:

1
sudo yum install expect

For Fedora systems:

On Fedora, you can install expect with:

1
sudo dnf install expect

Verifying Installation:

After installation, you can verify that expect has been installed by checking its version:

1
expect -v

🎯 Linux expect Command Options

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

Command Description
-c Specifies the command to execute before the script.
-d Provides a brief diagnostic output.
-D Interactive debugger.
-f Specifies a file to read from.
-i Prompts commands interactively.
-b Reads file line by line (buffer).
-v Print version.

The expect command is commonly used for automating interactive applications, such as SSH logins, by simulating user inputs.

📝 Basic Usage of expect

expect works by automating interactions with programs by scripting the responses. Here’s a basic structure of an expect script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/expect

# Set timeout (optional)
set timeout 20

# Spawn a command (e.g., SSH login)
spawn ssh user@hostname

# Expect a specific prompt
expect "password:"

# Send the response (password)
send "your_password\r"

# Interact with the session
interact

Explanation:

  • spawn: This is used to start a new process or command, such as SSH or FTP.
  • expect: Waits for a specific pattern or string to appear in the output of the spawned process.
  • send: Sends a response to the spawned process.
  • interact: Allows the user to interact with the spawned session after the automation is complete

🔐 Automating SSH Login with expect

One common use case for expect is automating SSH logins, especially when using ssh to connect to remote servers.

Here’s an example script to automate SSH login:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/expect

# Set variables
set timeout 20
set host "your.server.com"
set user "your_username"
set password "your_password"

# Spawn the SSH command
spawn ssh $user@$host

# Look for the password prompt
expect "password:"

# Send the password
send "$password\r"

# Interact with the session
interact

To use this script, simply save it to a file (e.g., ssh_login.expect), make it executable, and run it:

1
2
chmod +x ssh_login.expect
./ssh_login.expect

Note:

Storing passwords in plain text within scripts is not recommended for production environments due to security risks. Consider using SSH key-based authentication for more secure automation.

⚙️ Automating File Transfer with expect (FTP)

You can also use expect to automate FTP or SFTP commands. Here’s an example of automating an FTP login:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/expect

# Set variables
set timeout 20
set host "ftp.yourserver.com"
set user "your_username"
set password "your_password"

# Spawn FTP command
spawn ftp $host

# Look for the username prompt
expect "Name:"

# Send the username
send "$user\r"

# Look for the password prompt
expect "Password:"

# Send the password
send "$password\r"

# Perform FTP operations (e.g., uploading a file)
expect "ftp>"
send "put /path/to/local/file /path/to/remote/file\r"

# Exit FTP
expect "ftp>"
send "bye\r"

# Interact with the session
interact

🛠️ Using expect in Scripts

You can also integrate expect into larger shell scripts. For example, here’s how to automate multiple commands in a script:

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

# Run first expect script
expect ./ssh_login.expect

# Run second expect script
expect ./ftp_upload.expect

# More commands as needed

This allows you to use expect for complex automation tasks that involve multiple programs and interactions.

🐛 Debugging expect Scripts

If you’re running into issues with your expect scripts, you can enable debugging by adding the following line at the beginning of your script:

1
log_user 1

This will print all interactions and outputs to the terminal, making it easier to troubleshoot problems.

🔄 Autoexpect

Autoexpect records your session and generates an Expect script based on your interactions.

🔹 Step 1: Run Autoexpect with an Interactive Script

Let’s assume we have a simple interactive script (interactive_script.sh) that asks for user input:

1
2
3
4
#!/bin/bash
echo "Enter your username:"
read username
echo "Welcome, $username!"

Run Autoexpect with:

1
autoexpect ./interactive_script.sh

This will:

  • Execute the script.
  • Capture all interactions.
  • Save them into a new Expect script (script.exp).

🔹 Step 2: Provide Input as Required

Follow the script’s prompts, entering responses when needed. Autoexpect will capture everything.

Once finished, you’ll see:

1
Autoexpect generated "script.exp".

🔹 Step 3: Review Generated Script

Open the generated script.exp file to see how Autoexpect translated the interaction into an Expect script:

1
vim script.exp

You’ll find an automatically created Expect script that replicates your session.

Autoexpect is a powerful tool that eliminates the need for manually writing Expect scripts. By simply running your interactive tasks once, you generate a fully functional automation script, saving time and effort.

expect

📝 Conclusion

Expect is a versatile tool for automating tasks that involve interactive terminal programs, such as SSH logins, Telnet sessions, or file transfers via SCP/FTP.

By combining core commands like spawn, send, and expect, you can create powerful scripts to streamline repetitive tasks, saving time and reducing errors.

For more advanced usage, refer to the official Expect documentation or explore community resources like Stack Overflow.

This post is licensed under CC BY 4.0 by the author.