Exploitationautomationinteractivescriptingtcltestinggui

expect

Expect automates interactive applications by scripting expected prompts and responses. It enables testing of applications and wrapping them in X11 GUIs using Tk.

Description

Expect is a tool for automating interactive applications according to a script. Following the script, Expect knows what can be expected from a program and what the correct response should be. Expect is also useful for testing these same applications. An interpreted language provides branching and high-level control structures to direct the dialogue. The user can take control and interact directly when desired, afterward returning control to the script.

This package contains the expect binary and several Expect-based scripts including autoexpect, autopasswd, cryptdir, decryptdir, dislocate, kibitz, lpunlock, mkpasswd, and multixterm. These scripts provide specialized automation for tasks like generating Expect scripts from sessions, password management, directory encryption, process disconnection, multi-user shell sharing, and managing multiple xterms.

Expect is particularly valuable in cybersecurity for automating repetitive interactive tasks such as login sequences, protocol interactions, and testing application behaviors in controlled scripted environments.

How It Works

Expect operates as an extension to Tcl, spawning interactive programs and monitoring their output for expected patterns. When a match occurs, it sends predefined responses via the 'send' command. Control structures like expect, timeout, and interact allow branching based on output, timeouts, or user intervention. Timing issues are handled with sleeps or conservative mode pauses. Scripts can be generated automatically using autoexpect, which records user interactions and converts them into Expect syntax, accounting for echoing and prompts.

Installation

bash
sudo apt install expect

Flags

-fSpecify output file for generated script
-cStart autoexpect in conservative mode, pausing briefly before sending each character
-CDefine key to toggle conservative mode (e.g., -C ^L)
-pStart autoexpect in prompt mode, looking only for the last line of program output
-PDefine key to toggle prompt mode (e.g., -P ^P)
-quietDisable informational messages produced by autoexpect
-QName a quote character to enter toggle characters literally
-noprocRun kibitz with no process underneath for connecting multiple processes
-noescapeDisable the escape character in kibitz
-escapeSet the escape character in kibitz (default ^])
-xaArguments to pass to xterm in multixterm
-xcCommand to run in each xterm in multixterm
-xnName for each xterm in multixterm, substituted in command

Examples

Spawn a shell and generate an Expect script from the interactive session, saved to script.exp by default
autoexpect
Spawn ftp to ftp.cme.nist.gov and generate an Expect script from the interactive session
autoexpect ftp ftp.cme.nist.gov
Run ftp to ftp.cme.nist.gov and store the resulting Expect script in file nist
autoexpect -f nist ftp ftp.cme.nist.gov
Start autoexpect in conservative mode, pausing before sending each character to handle timing issues
autoexpect -c
Start autoexpect in conservative mode with ^L as toggle for conservative mode
autoexpect -c -C ^L
Start autoexpect in prompt mode, only looking for the last line of program output like prompts
autoexpect -p
Start kibitz to allow user2 to share a shell session with the current user
kibitz user2
Start two xterms using ssh to hosts bud and dexter, driven together or separately
multixterm -xc "ssh %n" bud dexter
Updated 2026-04-16kali.org ↗