Vous voulez voir cette page en français ? Cliquez ici.

Have one to sell? Sell yours here
Exploring Expect: A Tcl-based Toolkit for Automating Interactive Programs
 
 

Exploring Expect: A Tcl-based Toolkit for Automating Interactive Programs [Paperback]

Don Libes
3.5 out of 5 stars  See all reviews (22 customer reviews)

Available from these sellers.


‹  Return to Product Overview

Product Description

Book Description

Expect is quickly becoming a part of every UNIX user's toolbox. It allows you to automate Telnet, FTP, passwd, rlogin, and hundreds of other applications that normally require human interaction. Using Expect to automate these applications will allow you to speed up tasks and, in many cases, solve new problems that you never would have even considered before.

For example, you can use Expect to test interactive programs with no changes to their interfaces. Or wrap interactive programs with Motif-like front-ends to control applications by buttons, scrollbars, and other graphic elements with no recompilation of the original programs. You don't even need the source code! Expect works with remote applications, too. Use it to tie together Internet applications including Telnet, Archie, FTP, Gopher, and Mosaic.

Don Libes is the creator of Expect as well as the author of this book. In Exploring Expect, he provides a comprehensive tutorial on all of Expect's features, allowing you to put it immediately to work on your problems. In a down-to-earth and humorous style, he provides numerous examples of challenging real-world applications and how they can be automated using Expect to save you time and money.

Expect is the first of a new breed of programs based on Tcl, the Tool Command Language that is rocking the computer science community. This book provides an introduction to Tcl and describes how Expect applies Tcl's power to the new field of interaction automation. Whether your interest is in Expect or interaction automation or you simply want to learn about Tcl and see how it has been used in real software, you will find Exploring Expect a treasure trove of easy-to-understand and valuable information.

From the Publisher

Written by the author of Expect, this is the first book to explain how this part of the UNIX toolbox can be used to automate Telnet, FTP, passwd, rlogin, and hundreds of other interactive applications. Based on Tcl (Tool Command Language), Expect lets you automate interactive applications that have previously been extremely difficult to handle with any scripting language.

About the Author

Don Libes is married to Susan Mulroney, a professor in the Department of Physiology and Biophysics at the Georgetown University School of Medicine. Sue performs research in the area of kidney growth and development. Their well-hydrated daughter, Kenna, has two lovely kidneys.

Excerpt. © Reprinted by permission. All rights reserved.

Chapter 3 - Getting Started With Expect

Three commands are central to the power of Expect: send , expect , and spawn . The send command sends strings to a process, the expect command waits for strings from a process, and the spawn command starts a process.

In this chapter, I will describe these commands and another one that is very useful: interact . To best understand this chapter, it will help to have some basic familiarity with Tcl. If you are wondering about a command that is not explained, look it up in the index for a reference in the previous chapter and read about it there.

The send Command
The send command takes a string as an argument and sends it to a process. For example:

send "hello world"

This sends the string " hello world " (without the quotes). If Expect is already interacting with a program, the string will be sent to that program. But initially, send will send to the standard output. Here is what happens when I type this to the Expect interpreter interactively:

expect1.1> send "hello world"

hello worldexpect1.2>

The send command does not format the string in any way, so after it is printed the next Expect prompt gets appended to it without any space. To make the prompt appear on a different line, put a newline character at the end of the string. A newline is represented by " \n ".

expect1.1> send "hello world\n"
hello world

expect1.2>
If these commands are stored in a file, speak , the script can be executed from the UNIX command line:

% expect speak

hello world

%

With a little magic it is possible to invoke the file as just " speak " rather than " expect speak ". On most systems it suffices to insert the line " #!/usr/local/bin/expect -- " and say " chmod +x speak; rehash ". I will explain this in more detail in . For now, just take it on faith.

The expect Command
expect is the opposite of send . The expect command waits for a response, usually from a process. expect can wait for a specific string, but more often expect is used to wait for any string that matches a given pattern. Analogous to send , the expect command initially waits for characters from the keyboard1. Using them, I can create a little conversation:

expect "hi\n"

send "hello there!\n"

When run, the interaction looks like this:

hi

hello there!

I typed the string hi and then pressed return. My input matched the pattern " hi\n ". Ideally, a return would be matched with " \r "; however, the UNIX terminal driver translates a return to " \n ".2 As you will see later on, it is rarely necessary to have to worry about this mapping because most of Expect's interactions occur with programs not users, and programs do not "press return". Nonetheless, it is occasionally useful to expect input from people. Plus, it is much easier to experiment with Expect this way.

If expect reads characters that do not match the expected string, it continues waiting for more characters. If I had typed hello followed by a return, expect would continue to wait for " hi\n ".

When the matching string is finally typed, expect returns. But before returning, expect stores the matched characters in a variable called expect_out(0,string) . All of the matched characters plus the characters that came earlier but did not match are stored in a variable called expect_out(buffer) . expect does this every time it matches characters. The names of these variables may seem odd, but they will make more sense later on.

Imagine the following script:

expect "hi\n"

send "you typed "

send "but I only expected "

The angle brackets do not do anything special. They will just appear in the output, making it clear where the literal text stops and the variable values start. When run the script looks like this:

Nice weather, eh?

hi

you typed

but I only expected

I typed " Nice weather, eh " " hi " . expect reported that it found the hi but it also found something unexpected: " Nice weather, eh?\n ".

Anchoring
Finding unexpected data in the input does not bother expect . It keeps looking until it finds something that matches. It is possible to prevent expect from matching when unexpected data arrives before a pattern. The caret ( ^ ) is a special character that only matches the beginning of the input. If the first character of the pattern is a caret, the remainder of the pattern must match starting at the beginning of the incoming data. It cannot skip over characters to find a valid match. For example, the pattern ^hi matches hiccup but not sushi .

The dollar sign ( $ ) is another special character. It matches the end of the data. The pattern hi$ matches sushi but not hiccup . And the pattern ^hi$ matches neither sushi nor hiccup . It matches hi and nothing else.

Patterns that use the ^ or $ are said to be anchored . Some programs, such as sed , define anchoring in terms of the beginning of a line. This makes sense for sed , but not for expect . expect anchors at the beginning of whatever input it has received without regard to line boundaries.

When patterns are not anchored, patterns match beginning at the earliest possible position in the string. For example, if the pattern is hi and the input is philosophic , the hi in philo is matched rather than the hi in sophic . In the next section, this subtlety will become more important.

‹  Return to Product Overview