Expect |
Expect is a Unix automation and testing tool, written by Don Libes, for interactive applications such as Telnet, ftp, Passwd, Fsck, Rlogin, tip, ssh, and others. With Tk, interactive applications can be wrapped in X11 GUIs.
=Basics=
Expect is an extension of Tcl with two significant commands added: send, and expect. These allow a script to control command-line tools originally designed to interact only with a human. For example, programs which interactively prompt for a password cannot be automated with shell scripts or other languages but can be trivially automated with an Expect script.
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 significantly new power, flexibility, and reliability.
=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. In general, don t try to figure out how to solve a problem inside of Expect. Instead, figure out how to make Expect utilize the system s existing tools.
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 Scripting_programming_languages. 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.
=Opinion=
==Pros==
This author has used Expect to build monitor scripts running at regular intervals through cron on mission critical systems. The script encapsulates the reactions for a common set of situations, allowing the script to handle a sizable piece of system administration automatically. And by having the system automatically check this, it saves the administrator from having to poll the system manually.
Another feature is that 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 bonus features built in.
==Cons==
Expect inherits the same syntax convention as Tcl, which many people find confusing. Compared to some other scripting languages such as Bash, csh, and Perl, Expect has a different twist, and is sometimes challenging to remember when a variable must be prefixed with a $ , and when it is not.
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.
Also, 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.
=External links=
*[http://expect.nist.gov/ Official homepage] *[http://www.google.com/searchq=exploring+expect Exploring Expect by Don Libes]|
|