LinuxCommandLibrary

expect

Automate interactive applications

TLDR

Execute an expect script from a file

$ expect [path/to/file]
copy

Execute a specified expect script
$ expect -c "[commands]"
copy

Enter an interactive REPL (use exit or to exit)
$ expect -i
copy

SYNOPSIS

expect [-dDiqr] [-c cmds] [[-f] cmdfile] [args]

PARAMETERS

-d
    Enable debugging output.

-D
    Enable even more debugging output (developer debugging). (Rarely used)

-i
    Open an interactive Expect session, allowing for manual execution of Expect commands.

-q
    Run quietly, suppressing normal output.

-r
    Use raw mode for interacting with the spawned process. This avoids translating newline characters.

-c cmds
    Execute the Expect commands provided as a string 'cmds'.

[-f] cmdfile
    Execute Expect commands from the script file 'cmdfile'. If -f is omitted, Expect attempts to interpret the file path as an option first.

args
    Arguments passed to the spawned process.

DESCRIPTION

Expect is a powerful tool that automates interactive applications by providing a scripting language to control them. It allows you to write scripts that interact with programs that require human input, such as telnet, ftp, passwd, rlogin, and others. Expect works by spawning a process and then "expecting" certain output patterns from that process. Based on the received output, the script can then send appropriate commands or responses. This makes it invaluable for automating repetitive tasks, configuring systems, and testing interactive programs.

At its core, Expect enables you to write scripts that mimic human interaction with command-line interfaces. By defining expectations for specific prompts or outputs, the script can automatically provide the correct responses, essentially creating an automated dialogue with the target application. This ability can also be used in conjunction with other tools like SSH to automatically configure multiple computers or automate complex installation processes.

CAVEATS

Expect relies on predictable output patterns from the target application. If the output changes, the script may fail. Also, error handling and robust pattern matching are crucial for writing reliable Expect scripts.

KEY COMMANDS

Important Expect commands include: spawn (starts a new process), expect (waits for a specific pattern), send (sends commands to the process), interact (hands control to the user), set (assigns variables), and close (closes the spawned process).

PATTERN MATCHING

The expect command uses regular expressions to match patterns in the spawned process's output. Mastering regular expressions is essential for writing effective Expect scripts. Common patterns include exact strings, wildcards, and character classes.

TIMEOUT

The expect command also supports a timeout parameter. If the expect command does not find expected result within the time limit, it continues with the default action or specified action via "timeout" keyword.

HISTORY

Expect was created by Don Libes at the National Institute of Standards and Technology (NIST) in the early 1990s. It was designed to address the need for automating interactions with programs that were not designed to be controlled programmatically. Since its creation, Expect has become a widely used tool for system administration, network automation, and testing interactive applications. Its ability to automate complex tasks that previously required manual intervention has made it indispensable in many environments.

SEE ALSO

tclsh(1), ssh(1)

Copied to clipboard