Samiksha Jaiswal (Editor)

Expect

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Original author(s)
  
Don Libes

Operating system
  
POSIX, Windows

Website
  
expect.sourceforge.net

Written in
  
Tcl

License
  
Public domain

Stable release
  
5.45.1 / 15 August 2012 (2012-08-15)

Expect, an extension to the Tcl scripting language written by Don Libes, is a program to automate interactions with programs that expose a text terminal interface. Expect was originally written in 1990 for Unix systems, but is now also available for Microsoft Windows and other systems. It 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.

Contents

Basics

Expect has regular expression pattern matching and general program capabilities, allowing simple scripts to intelligently control programs such as telnet, ftp, and ssh, all of which lack a programming language, macros, or any other program mechanism.

Usage

Expect serves as a "glue" to link existing utilities together. The general idea is to try to figure out how to make Expect utilize 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 looking up information inside the product's command-line interface, the script can make an intelligent decision 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 feed back 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, whether it be for programs, utilities or embedded systems. DejaGnu is a testing suite written using Expect for use in testing. It has been used extensively for testing gcc and is very well suited to testing remote targets such as embedded development.

One can automate the generation of an expect script 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 password):

it should be noted that 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 in user machine:

Alternatives

Various projects implement Expect-like functionality in other languages, such as C#, Java, Perl, Python and Ruby. 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
  • Java

  • 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 clousures. There is also wrapper for Groovy language DSL.
  • Scala

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

  • expect4groovy  - a Groovy DSL implementation of Expect tool.
  • 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
  • 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
  • Shell

  • Empty — expect-like utility to run interactive commands in the UNIX shell-scripts
  • References

    Expect Wikipedia