Linux expect Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Linux expect command to automate interactive command-line applications, such as SSH, FTP, and more. You will start by understanding the purpose and syntax of the expect command, then move on to automating SSH login and handling prompts and responses in expect scripts.

The lab covers the following steps:

  • Understand the Purpose and Syntax of the expect Command
  • Automate SSH Login with expect
  • Handle Prompts and Responses in Expect Scripts

The expect command is a powerful tool that allows you to create scripts to automate repetitive tasks, reducing the need for manual intervention. In the first step, you will install the expect package and explore the basic syntax of the expect command. In the subsequent steps, you will learn how to use expect to automate SSH login and handle various prompts and responses in your scripts.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/RemoteAccessandNetworkingGroup(["`Remote Access and Networking`"]) linux/RemoteAccessandNetworkingGroup -.-> linux/ssh("`Secure Connecting`") subgraph Lab Skills linux/ssh -.-> lab-422669{{"`Linux expect Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the expect Command

In this step, you will learn about the purpose and syntax of the expect command in Linux. The expect command is a powerful tool that allows you to automate interactive command-line applications, such as SSH, FTP, and more.

The expect command works by interacting with a program, sending it input, and then checking the output for expected responses. This allows you to create scripts that can automate repetitive tasks, reducing the need for manual intervention.

Let's start by installing the expect package in our Ubuntu 22.04 environment:

sudo apt-get update
sudo apt-get install -y expect

Now, let's explore the basic syntax of the expect command:

expect [options] [script file]

The expect command can be used in two ways:

  1. Interactive mode: You can start the expect command without any arguments, and it will enter an interactive mode where you can type commands and see the output.
expect
  1. Script mode: You can provide a script file as an argument to the expect command, and it will execute the commands in the script.
expect my_script.exp

In the script mode, the expect command uses a domain-specific language to define the expected output and the corresponding actions to take. The basic structure of an expect script is as follows:

#!/usr/bin/expect -f

set timeout 10
spawn ssh user@host
expect "password:"
send "mypassword\r"
expect "$"
send "ls -l\r"
expect eof

In this example, the expect script:

  • Sets a timeout of 10 seconds
  • Spawns an SSH session to the remote host
  • Waits for the "password:" prompt
  • Sends the password
  • Waits for the shell prompt
  • Executes the ls -l command
  • Waits for the end of the script (eof)

This is just a simple example, but the expect command can be used to automate much more complex interactions.

Automate SSH Login with expect

In this step, you will learn how to use the expect command to automate the SSH login process.

Let's start by creating an expect script to automate the SSH login:

#!/usr/bin/expect -f

set timeout 10
set host "example.com"
set user "myuser"
set password "mypassword"

spawn ssh $user@$host
expect "password:"
send "$password\r"
expect "$"
send "echo 'SSH login successful!'\r"
expect eof

In this script:

  • We set the timeout to 10 seconds
  • We define the host, user, and password variables
  • We spawn the SSH session using the spawn command
  • We wait for the "password:" prompt using the expect command
  • We send the password using the send command
  • We wait for the shell prompt using the expect command
  • We execute the echo command to verify the SSH login
  • We wait for the end of the script using the expect eof command

Save this script as ssh_login.exp in your ~/project directory.

Now, let's make the script executable and run it:

chmod +x ~/project/ssh_login.exp
~/project/ssh_login.exp

You should see the output:

SSH login successful!

This demonstrates how the expect command can be used to automate the SSH login process, eliminating the need for manual intervention.

Handle Prompts and Responses in Expect Scripts

In this step, you will learn how to handle different types of prompts and responses in your expect scripts.

Let's create a new expect script that demonstrates handling various prompts and responses:

#!/usr/bin/expect -f

set timeout 10
set host "example.com"
set user "myuser"
set password "mypassword"

spawn ssh $user@$host
expect {
    "password:" {
        send "$password\r"
        expect "$"
    }
    "yes/no" {
        send "yes\r"
        expect "$"
    }
    "$" {
        send "echo 'SSH login successful!'\r"
        expect "$"
    }
    timeout {
        puts "SSH login timed out"
        exit 1
    }
}

send "exit\r"
expect eof

In this script, we use the expect command with a block of expect statements to handle different types of prompts:

  1. If the script encounters the "password:" prompt, it sends the password and waits for the shell prompt.
  2. If the script encounters a "yes/no" prompt, it sends "yes" and waits for the shell prompt.
  3. If the script encounters the shell prompt ($), it executes the echo command to verify the SSH login.
  4. If the script encounters a timeout, it prints a message and exits with an error code.

Finally, the script sends the exit command to close the SSH session and waits for the end of the script (expect eof).

Save this script as handle_prompts.exp in your ~/project directory.

Now, let's make the script executable and run it:

chmod +x ~/project/handle_prompts.exp
~/project/handle_prompts.exp

You should see the output:

SSH login successful!

This demonstrates how the expect command can be used to handle different types of prompts and responses in your scripts, making them more robust and adaptable.

Summary

In this lab, you learned about the purpose and syntax of the expect command in Linux. The expect command is a powerful tool that allows you to automate interactive command-line applications, such as SSH, FTP, and more. You learned how to install the expect package and explore the basic syntax of the expect command, including the interactive mode and script mode. Additionally, you learned how to use the expect command to automate SSH login, handling prompts and responses in expect scripts.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like