LinuxCommandLibrary

autoexpect

Generate Expect scripts from interactive sessions

TLDR

Spawn a shell and generate a script from it

$ autoexpect
copy

Run a command and generate a script from it
$ autoexpect [command]
copy

Run a command, generate a script, and save the script to a specified file
$ autoexpect -f [path/to/file] [command]
copy

SYNOPSIS

autoexpect [-f outputfile] [-g pattern] [-p pattern] [-q] [-v] [command [args ...]]

PARAMETERS

-f file
    Specify output file for the generated Expect script (default: script.exp in current directory).

-g pattern
    Glob pattern that the command name must match; useful for pattern-matching in logs or multiple runs.


-p pattern
    Expect this exact pattern immediately after starting; skips initial auto-detection.


-q
    Quiet mode; suppresses interactive questions about detected patterns.


-v
    Verbose mode; prints additional debugging information during recording.


DESCRIPTION

autoexpect is a utility from the Expect scripting toolkit designed to simplify the creation of Expect scripts by automatically recording interactive terminal sessions and generating corresponding automation scripts.

It launches a specified command (or /bin/sh by default), captures all user input and program output in real-time, and uses heuristics to infer appropriate expect patterns, such as prompts or responses. The resulting script can then be edited and replayed to automate repetitive interactive tasks, like telnet logins, ftp sessions, or configuration tools.

During recording, autoexpect prompts the user to verify patterns it detects, allowing interactive refinement. It handles timing with exp_internal for debugging and escapes special characters. This makes it invaluable for prototyping before manual scripting, though the generated code often requires tweaks for robustness, variable handling, or error conditions.

Integrated with Tcl/Expect, it's ideal for sysadmins and developers automating CLI interactions without deep scripting knowledge upfront.

CAVEATS

Generated scripts may embed sensitive data like passwords literally; edit before use.
Relies on heuristics, so complex sessions (e.g., timeouts, menus) often need manual fixes.
Records exact timings which may fail on slower systems; use expect_before for reliability.

USAGE TIPS

Run without args for shell session: autoexpect. Verify patterns interactively, then refine output script with send_user or timeouts.

Example: autoexpect -f myscript.exp ftp ftp.example.com records FTP login.

BACKSLASH ESCAPING

By default, escapes backslashes in output; some versions have undocumented -b for literal mode, but verify manpage.

HISTORY

Developed by Don Libes as part of the Expect extension to Tcl (1990); autoexpect introduced in Expect 5.0 (circa 1994) to ease script prototyping amid growing automation needs in Unix environments.

SEE ALSO

expect(1), tclsh(1), script(1)

Copied to clipboard