GlyphSignal

Expect

Extension to the Tcl scripting language

5 min read

Expect is an extension to the Tcl scripting language written by Don Libes. The program automates interactions with programs that expose a text terminal interface. Expect, originally written in 1990 for the Unix platform, has since become available for Microsoft Windows and other systems.

Basics

Expect is used to automate control of interactive applications such as Telnet, FTP, passwd, fsck, rlogin, tip, SSH, and others. Expect uses pseudo terminals (Unix) or emulates a console (Windows), starts the target program, and then communicates with it, just as a human would, via the terminal or console interface. Tk, another Tcl extension, can be used to provide a GUI.

Usage

Expect serves as a "glue" to link existing utilities together. The general idea is to figure out how to make Expect use the system's existing tools rather than figure out how to solve a problem inside of Expect.

A key usage of Expect involves commercial software products. Many of these products provide some type of command-line interface, but these usually lack the power needed to write scripts. They were built to service the users administering the product, but the company often does not spend the resources to fully implement a robust scripting language. An Expect script can spawn a shell, look up environmental variables, perform some Unix commands to retrieve more information, and then enter into the product's command-line interface armed with the necessary information to achieve the user's goal. After retrieving information by interacting with the product via its command-line interface, the script can make intelligent decisions about what action to take, if any.

Every time an Expect operation is completed, the results are stored in a local variable called $expect_out. This allows the script to harvest information to feedback to the user, and it also allows conditional behavior of what to send next based on the circumstances.

A common use of Expect is to set up a testing suite for programs, utilities or embedded systems. DejaGnu is a testing suite written using Expect for use in testing. It has been used for testing GCC and remote targets such as embedded development.

Expect script can be automated using a tool called 'autoexpect'. This tool observes your actions and generates an Expect script using heuristics. Though generated code may be large and somewhat cryptic, one can always tweak the generated script to get the exact code.

Another example is a script that automates FTP:

Below is an example that automates SFTP (with a password):

Using passwords as command-line arguments, like in this example, is a huge security hole, as any other user on the machine can read this password by running "ps". You can, however, add code that will prompt you for your password rather than giving your password as an argument. This should be more secure. See the example below.

Another example of automated SSH login to a user machine:

Alternatives

Various projects implement Expect-like functionality in other languages, such as C#, Java, Scala, Groovy, Perl, Python, Ruby, Shell and Go. These are generally not exact clones of the original Expect, but the concepts tend to be very similar.

C#

  • Expect.NET — Expect functionality for C# (.NET)
  • DotNetExpect — An Expect-inspired console automation library for .NET

Erlang

  • lux - test automation framework with Expect style execution commands.

Go

  • GoExpect - Expect-like package for the Go language
  • go-expect - an Expect-like Go language library to automate control of terminal or console based programs.

Groovy

  • expect4groovy  - a Groovy DSL implementation of Expect tool.

Java

  • ExpectIt — a pure Java 1.6+ implementation of the Expect tool. It is designed to be simple, easy to use and extensible.
  • expect4j — an attempt at a Java clone of the original Expect
  • ExpectJ — a Java implementation of the Unix expect utility
  • Expect-for-Java — pure Java implementation of the Expect tool
  • expect4java  - a Java implementation of the Expect tool, but supports nested closures. There is also wrapper for Groovy language DSL.

Perl

  • Expect.pm — Perl module (newest version at metacpan.org)

Python

  • Pexpect — Python module for controlling interactive programs in a pseudo-terminal
  • winpexpect — port of pexpect to the Windows platform
  • paramiko-expect — A Python expect-like extension for the Paramiko SSH library which also supports tailing logs.

Ruby

  • RExpect — a drop in replacement for the expect.rb module in the standard library.
  • Expect4r — Interact with Cisco IOS, IOS-XR, and Juniper JUNOS CLI

Rust

  • rexpect - pexpect-like package for the Rust language.

Scala

  • scala-expect — a Scala implementation of a very small subset of the Expect tool.

Shell

  • Empty — expect-like utility to run interactive commands in the Unix shell-scripts
  • sexpect — Expect for shells. It's implemented in the client/server model which also supports attach/detach (like GNU screen).

References

Further reading

  • Libes, Don (1995). Exploring Expect: A Tcl-Based Tool for Automating Interactive Programs. O'Reilly Media. ISBN 1-565-92090-2.
  • Fisher, David (1999). "Advanced Programming in Expect: A Bulletproof Interface". Linux Gazette, (a member of the Linux Documentation Project (LDP)). No. 48. issue of the Linux Gazette.

External links

  • Official website
  • Expect on SourceForge
  • When to use Expect scripting and when to avoid it at the Wayback Machine (archived 2017-10-25) (IBM Developerworks)
Read full article on Wikipedia →

Content sourced from Wikipedia under CC BY-SA 4.0

Share

Keep Reading

2026-02-24
2
Robert Reed Carradine was an American actor. A member of the Carradine family, he made his first app…
1,253,437 views
4
Nemesio Rubén Oseguera Cervantes, commonly referred to by his alias El Mencho, was a Mexican drug lo…
453,625 views
5
David Carradine was an American actor, director, and producer, whose career included over 200 major …
381,767 views
6
Keith Ian Carradine is an American actor. In film, he is known for his roles as Tom Frank in Robert …
339,326 views
7
.xxx is a sponsored top-level domain (sTLD) intended as a voluntary option for pornographic sites on…
290,593 views
8
Ever Carradine is an American actress. She is known for her roles as Tiffany Porter and Kelly Ludlow…
289,538 views
Continue reading: