expect
Automate interactive applications
TLDR
Execute an expect script from a file
Execute a specified expect script
Enter an [i]nteractive REPL (use exit or
SYNOPSIS
expect [-c command] [-d] [-D debug_flags] [-f] [-i] [-N] [-P interpreter] [-B] [-L logfile] [-l] [-s] [-b] [-t timeout] [--] [script] [script_args]
PARAMETERS
-c command
Executes the specified command string as an Expect script. Useful for one-liners.
-d
Enables debugging output, showing all spawn, expect, and send actions.
-D debug_flags
Enables specific debugging modes (e.g., 0x01 for Tcl stack trace, 0x02 for Expect internal trace).
-f
Forces execution of the script even if it seems insecure (e.g., world-writable).
-i
Forces interactive mode, even if stdout is not a TTY.
-N
Prevents Expect from parsing script arguments, passing them directly to the script.
-P interpreter
Specifies an alternative Tcl interpreter.
-B
Enables batch mode, preventing stdout buffering.
-L logfile
Logs all input and output to the specified logfile.
-l
Same as -L.
-s
Enables synchronous output, flushing stdout after every print.
-b
Buffers input, reading more than just one character at a time.
-t timeout
Sets the default timeout for expect commands in seconds.
--
Marks the end of command-line options, useful if the script name starts with a hyphen.
script
The path to the Expect script file to be executed.
script_args
Arguments passed to the Expect script.
DESCRIPTION
expect is a powerful Tcl-based scripting language designed to automate interactive command-line programs. It "expects" specific output from a program and "sends" input in response, mimicking human interaction. This is particularly useful for tasks that require user input, such as logging into remote servers (ssh), transferring files (scp, ftp), or running installation scripts. By scripting these interactions, expect enables batch processing, unattended execution, and robust automation of previously manual, interactive workflows, significantly improving efficiency and reducing human error.
CAVEATS
Security Risks:
Passing sensitive information like passwords directly in scripts or as arguments is insecure. Consider using secure credential management systems.
Fragility:
Expect scripts are sensitive to changes in the interactive program's prompts or output. Minor changes can break a script.
Debugging Complexity:
Debugging can be challenging, especially with complex interactions and timeouts. The -d and -D flags are crucial.
Resource Usage:
For very high-volume or long-running tasks, expect can consume more resources than a non-interactive solution, if one exists.
CORE COMMANDS
The power of expect lies in its core commands:
spawn:
Starts a new process and attaches it to Expect's control.
expect:
Waits for a specific pattern (string or regex) from the spawned process's output.
send:
Sends a string of characters (including special characters like newlines) to the spawned process's input.
interact:
Relinquishes control to the user, allowing direct interaction with the spawned process until a specific key sequence or pattern is encountered.
PATTERN MATCHING
expect uses powerful pattern matching capabilities, including glob-style and regular expressions, to identify specific outputs from programs, making it highly flexible in responding to various prompts.
TIMEOUT HANDLING
Each expect command can have a timeout. If the expected pattern is not received within the timeout period, the script can handle the condition, preventing infinite waits.
HISTORY
The expect utility was created by Don Libes at the National Institute of Standards and Technology (NIST) in the early 1990s (around 1990-1991). It was developed to address the significant challenge of automating interactive programs, which previously required manual human intervention. Built on the Tcl (Tool Command Language) scripting language, expect provided a robust and flexible framework for sending inputs and waiting for specific outputs, revolutionizing the automation of tasks like remote logins, software installations, and batch processing. Its widespread adoption solidified its role as an indispensable tool for system administrators and developers.