Jump to ratings and reviews
Rate this book

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

Rate this book
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.

602 pages, Paperback

First published December 1, 1994

14 people are currently reading
49 people want to read

About the author

Don Libes

8 books8 followers

Ratings & Reviews

What do you think?
Rate this book

Friends & Following

Create a free account to discover what your friends think of this book!

Community Reviews

5 stars
26 (36%)
4 stars
25 (35%)
3 stars
13 (18%)
2 stars
4 (5%)
1 star
3 (4%)
Displaying 1 - 4 of 4 reviews
Profile Image for Will Semin.
17 reviews15 followers
Read
November 18, 2019
Quite a nice book. There were one or two things that I haven't seen in other books so far, but I had a gut FEELING that it's the right way to go. There were a couple of things that I am not really convinced about, but maybe it all depends on the context. All in all, I think it was well written and worth reading.
Profile Image for Jerry.
Author 10 books27 followers
April 15, 2020

It is almost always easier to fix problems if you made some attempt at detecting them in the first place.


I ran into Expect as the solution to a problem many years ago, probably in the mid-2000s, and I thought at the time that it seemed like a good idea to learn it more. But I put it on the back burner until I happened to see this book in a used bookstore’s discount rack in Raleigh, North Carolina.

I think it remains worth knowing, and is likely to be a useful part of a serious scripting toolbox.

Expect is mainly for scripting poorly-designed programs that (a) don’t timeout well, or (b) require user input instead of command-line switches or (in the case of passwords) some form of automating the acquisition of them without sticking them on the command line. These problems are much rarer now than when this book was written.

The age of the book—it was published in January 1995, which means it was probably written over 1994—means that many of the examples are outdated. They tend to involve telnet, login, rsh, ftp, and even finger. The first three don’t exist on my Macintosh, and I’m slightly surprised that the last one does. The examples provided for these, especially ftp and rsh, are unnecessary in modern sftp and ssh.

And of course programs change over time, so that while the script that attempts to re-run rogue until it generates a strength of 18 works, it never ends, because rogue no longer generates a strength of 18, or if it does, it is much less often than the one-in-twenty chance the author was expecting in 1995.

Even more outdated is the mindset behind some of the examples, such as the “passmass” script that automates setting your password to the same thing on every host you have a login to. This script is not provided in the book, it is merely described, because the script was part of the standard Expect installation. It doesn’t appear to be now, unless I don’t have a complete installation.

The examples use, rather than example.com addresses, existing sites at the time, such as next.com or uunet.uu.net. (While the publisher lists the release date as August 2010 on their ebook site, this appears to be simply the date the ebook was released—the snippets available and the page count match the 1995 print version exactly.)

A few of the examples would still be useful for system administrators in special cases, such as scripting ping, but even ping now includes functionality that comes close to what the examples do.

One intriguing example involves bc, the command-line calculator. It was very difficult for me to write the Perl script padmath (that I included in 42 Astounding Scripts) to make bc accessible as a service on the Mac, because bc remains unsuitable for interaction with scripts. It looks like padmath would have been easier to write and more reliable in Expect.

Another interesting example is using Expect to make the command-line chess script play against itself. Expect can be used like putting two telephones together to connect different programs that each are expecting user input.

A lot of the need for expect has gone. Command-line programs are a lot smarter now, and the options are greater. The successors to ftp and telnet are very scriptable, and in a more secure fashion than storing the password in a script-accessible fashion; sudo probably didn’t even exist at the time Libes wrote this book—while sudo existed before 1994, it’s unclear to me how widespread it was—and the spread of web-based REST APIs that are easily accessed via scripts was still well in the future. The web was only a few years old and is barely mentioned. He mentions toward the beginning that some of the documentation for Expect is on the WWW, and he briefly mentions Mosaic toward the end.

Some of the examples even involve dialup, and there’s a special variable that controls how fast Expect outputs text, because dialup often lost data when data was sent too quickly. Even more interesting is that this functionality is augmented by the ability to act like a human, with a variable rate of typing.

There’s a technique he uses in writing this book that I find annoying, which is that he’ll produce Expect code for performing some common task, describe how the code works and why, and then says something like, this task is so common there’s a built-in way to do it in Expect, so you don’t need all that code.

What this means is that there are several code chunks in the book that you do not want to use. Reading the book through, that’s fine, because you’ll read the whole text. But if you use it as a reference, you might see the code chunk and not the explanation that there’s a better way of doing it.

He also provides code snippets that simply don’t work. For example, in the chapter on “interact”, there is code that, if attempted, provides the error “cannot interact with self”. The solution is to add “spawn $env(SHELL)” before the “interact” line.

This really does make me want to find reasons to write Expect scripts. It seems especially useful for its ability to mediate between the operator and the shell. One script I took from the book and have put into my toolbox is one that asks for user input, but with a timeout, so that the script can ask for input but not get locked up if there is no answer. This seems like it ought to be useful.


#!/usr/bin/expect --
# wait x seconds for a response and return it
# timedRead
# Don Libes, Exploring Expect, p. 77
# Modifications Jerry Stratton
# astoundingscripts.com

#help text
if {$argc != 1 || [lsearch $argv "--help"] > -1} {
puts "Usage: $argv0 "
puts "\twait seconds for a typed response, and return that response"
exit 1
}

set timeout $argv
expect "\n" {
send [string trimright "$expect_out(buffer)" "\n"]
}


Another idea is a script that itself starts up the default shell, continuing to allow normal shell commands, but that which logs the time spent on each command to a file. I have a handful of commands, mainly backup commands, that I run weekly and am always forgetting to time them, either by hand or by using the built-in command-line timer.

Interact seems like a powerful tool for customizing both the shell and any command-line app.

Expect uses Tcl as its language, and this book provides a decent amount of info on how to use Tcl, how to debug Tcl, how to use Tk, and how to use Tcl’s globs and regular expressions.

I need to reiterate that this is a scripting book from 1995. The fact that a quarter-century-old scripting tutorial is useful at all is amazing, and many of its flaws were not widely considered flaws at the time. Many of my complaints boil down to the book not being designed for using the examples while following along. In 1995, in order to follow along on the command line while reading this book I would either have had to go to work early, stay at work late, read it during work hours, or keep my telephone tied up while reading it at home, just for the occasional desire to type something or play around with the concepts.

The fact that my current off-the-shelf user-friendly home computer has Unix built into it is amazing. That my home computer sports as much of a command-line environment as a server from 1995 is also amazing. Both combine to obscure that this book was written for an entirely different era and a nearly entirely different computing environment. My own 42 Astounding Scripts will not be half as useful in 2044 as this book is in 2020.


All sorts of sins can be covered up in this way.
Profile Image for carl  theaker.
937 reviews52 followers
February 16, 2011


One of those many standard O'Reilly and associates
texts that you need to get by in the programming world,
this one, Expect is particularly good for testing software
and hardware. Plenty of examples which I found useful.
Displaying 1 - 4 of 4 reviews

Can't find what you're looking for?

Get help and learn more about the design.