ArticleslgStudy

computer science

Expect

Expect is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Expect rather than just read about it. In short: 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.

Key takeaways

  • Expect belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Expect to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Expect from memory before moving on to harder problems.

Reference excerpt

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)

Worked examples

Example 1 — a first encounter with Expect

Start with the simplest possible case. Write down what Expect claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Expect before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Expect ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Expect

In research
Expect appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Expect in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Expect is common in secondary-school and first-year university syllabi. It links to neighbouring topics Automation software, Free software programmed in Tcl, Public-domain software with source code, so understanding it makes those chapters shorter.
In everyday life
Look for Expect outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Expect” →

How to study Expect in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Expect means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Expect out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Expect in simple terms?

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.

Why does Expect matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Expect?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Expect.

Tags

  • Automation software
  • Free software programmed in Tcl
  • Public-domain software with source code
  • Scripting languages
  • Tk (software)

Keep exploring