Expect

Expect

Infobox Software
name = Expect
author = Don Libes
latest_release_version = 5.44.1
latest_release_date = January 31 2006
programming language = Tcl
operating_system = POSIX, Windows
license = Public domain [cite web | url=http://expect.nist.gov/FAQ.html#q6 |title=Expect FAQ: Our company policy requires a license to use Expect. Where can we get a license? ]
website = http://expect.nist.gov/

Expect is a Unix automation and testing tool, written by Don Libes as an extension to the Tcl scripting language, for interactive applications such as telnet, ftp, passwd, fsck, rlogin, tip, ssh, and others. It uses Unix pseudo terminals to wrap up subprocesses transparently, allowing the automation of arbitrary applications that are accessed over a terminal. With Tk, interactive applications can be wrapped in X11 GUIs.

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. The result is that Expect scripts provide old tools with significant new power and flexibility.

Examples

A simple example is a script that automates a telnet session:

# Assume $remote_server, $my_user_id, $my_password, and $my_command were read in earlier # in the script.
# Open a telnet session to a remote server, and wait for a username prompt. spawn telnet $remote_server expect "username:"
# Send the username, and then wait for a password prompt. send "$my_user_id " expect "password:"
# Send the password, and then wait for a shell prompt. send "$my_password " expect "%"
# Send the prebuilt command, and then wait for another shell prompt. send "$my_command " expect "%"
# Capture the results of the command into a variable. This can be displayed, or written to disk. set results $expect_out(buffer)
# Exit the telnet session, and wait for a special end-of-file character. send "exit " expect eof

Another example is a script that automates ftp:

# Open an ftp session to a remote server, and wait for a username prompt. spawn ftp $remote_server expect "username:"
# Send the username, and then wait for a password prompt. send "$my_user_id " expect "password:"
# Send the password, and then wait for an ftp prompt. send "$my_password " expect "ftp>"
# Switch to binary mode, and then wait for an ftp prompt. send "bin " expect "ftp>"
# Turn off prompting. send "prompt " expect "ftp>"
# Get all the files send "mget * " expect "ftp>"
# Exit the ftp session, and wait for a special end-of-file character. send "bye " expect eof

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 doesn't spend the resources to implement a fully 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 both 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, 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.

Opinion

Pros

Expect can be run at regular intervals through the use of cron to encapsulate system administration tasks. This works because Expect merely uses system administration tools already located on the host computer. No extra tools need to be learned. If the programmer has already learned Tcl, then migrating to Expect is a relatively easy transition. The same programming structures and syntax exist, but with additional features built in.

There is large support in the industry for using Expect for many in-house administration tasks. It is widely used by companies such as Silicon Graphics, IBM, HP, Sun, Xerox, Amdahl, Tektronix, AT&T, ComputerVision and the World Bank to run in-house automated testing for development projects, file transfers, account administration, and network testing.

Expect has been ported to Python and Perl languages in various add-on module projects. A subset of Expect commands has been ported to Java and is embedded within SwitchTermJ (java-based Terminal Emulator). Subroutines generally are an interpretation of the original version - with equivalent functionality. Once one understands the concept, one can trivially move to other languages as needed.

Cons

Expect inherits the same syntax convention as Tcl, which may seem unfamiliar if used to other script languages. Compared to languages such as bash, csh, and Perl, Expect has a different twist. It is sometimes challenging to remember when a variable must be prefixed with a "$", and when it must not. There are versions of Expect available for Perl and Python for those familiar with their syntax.

Another limitation is the difficulty in porting Expect scripts between platforms. For example, an Expect script that was written to use several Unix-based tools, might not be suitable if migrated to a Windows platform. If possible, the programmer must find counterpart command-line applications that provide the same information, and this will probably require changing the send/expect's, which can be a major part of the script. This is not an issue if you load tcl, perl or python on the machines in question, and use those languages' native POSIX interfaces for accessing files, and standard POSIX utilities (telnet, ftp etc.) for remote interaction.

A less obvious argument against Expect is that it can enable sub-optimal solutions. For example, a systems administrator needing to log into multiple servers for automated changes might use Expect with stored passwords, rather than the better solution of ssh agent keys. The ability to automate interactive tools is attractive, but there are frequently other options that can accomplish the same tasks in a more robust manner.

Expect automates command-line tools, not GUI-based tools. While Windows offers many valuable tools, many are GUI-based and thus outside the reach of Expect. GUI programs in Windows can be automated with tools like Autohotkey or AutoIt.

References

Further reading

* cite book
last = Libes
first = Don
year = 1995
title = Exploring Expect: A Tcl-Based Tool for Automating Interactive Programs
publisher = O'Reilly & Associates, Inc
id = ISBN 1-56592-090-2

External links

* [http://sourceforge.net/projects/expect/ Expect on SourceForge (current)]
* [http://expect.nist.gov/ Official homepage ("very outdated!")]
* [http://wiki.tcl.tk/201 The Tcler's Wiki -- Expect page]
* [http://sourceforge.net/projects/expectperl Perl Expect.pm module]
* [http://pexpect.sourceforge.net Pexpect a Pure Python Expect-like module]
* [http://www.gnulamp.com/expect.html Expect Scripting Tutorial]
* [http://empty.sourceforge.net Empty - expectlike tool to run command-line interactive programs in UNIX shell-scripts]
* [http://expect-lite.sourceforge.net/ Expect-lite -- a wrapper for expect, making automation easy, quick, and fun]
* [http://www.cotse.com/dlf/man/expect/bulletproof1.htm Bulletproof: Reliable CLI interface using Expect]
* [http://expectj.sourceforge.net/ ExpectJ - a Java implementation of the Unix expect utility]


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?
Synonyms:

Look at other dictionaries:

  • Expect — Тип инструмент для автоматизации и тестирования интерактивных приложений Разработчик Don Libes Написана на Tcl Операционная система Кроссплатформенное программное обеспечение Последняя версия 5.44.1 (31 января 2006 года) …   Википедия

  • Expect — Développeur Don Libes Dernière version …   Wikipédia en Français

  • Expect — Ex*pect , v. t. [imp. & p. p. {Expected}; p. pr. & vb. n. {Expecting}.] [L. expectatum, to look out for, await, expect; ex + out spectare to look at. See {Spectacle}.] 1. To wait for; to await. [Obs.] [1913 Webster] Let s in, and there expect… …   The Collaborative International Dictionary of English

  • expect — expect, hope, look, await are comparable when they mean to have something in mind as more or less certain to happen or come about. They vary, however, so greatly in their implications and in their constructions that they are seldom… …   New Dictionary of Synonyms

  • expect — [ek spekt′, ikspekt′] vt. [L expectare, exspectare < ex , out + spectare, to look, freq. of specere, to see: see SPECTACLE] 1. to look for as likely to occur or appear; look forward to; anticipate [I expected you sooner] 2. to look for as due …   English World dictionary

  • expect — was the object of much criticism during the 19c when it was used to mean ‘to suppose, surmise’, as in I expect you d like a drink. Fowler, however, regarded it as a natural extension of meaning and wrote (1926) that ‘it seems needless purism to… …   Modern English usage

  • expect — [v1] believe strongly; anticipate apprehend, assume, await, bargain for, bargain on, be afraid, calculate, conjecture, contemplate, count on, divine, envisage, feel, figure, forecast, foreknow, foresee, gather, hope, hope for, imagine, in the… …   New thesaurus

  • Expect — Ex*pect , v. t. To wait; to stay. [Obs.] Sandys. [1913 Webster] …   The Collaborative International Dictionary of English

  • Expect — Ex*pect , n. Expectation. [Obs.] Shak. [1913 Webster] …   The Collaborative International Dictionary of English

  • expect — I (anticipate) verb await, bargain for, be certain, be confident, be prepared, calculate upon, count on, expectare, have in prospect, look for, look forward to, plan on, prepare for, provide for, reckon on, sperare, wait for, watch for associated …   Law dictionary

  • expect — 1550s, wait, defer action, from L. expectare/exspectare await, look out for, desire, hope, from ex thoroughly (see EX (Cf. ex )) + spectare to look, frequentative of specere to look at (see SCOPE (Cf. scope) (1)). Figurative sense of …   Etymology dictionary

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”