oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Network Test Automation with Mac OS X and Tcl
Pages: 1, 2, 3, 4

An Expect scripting example

So what's all this talk about automating a console login? Let's take a look at a scripting example for automating an FTP remote login. This should shed some light on what Expect offers. Here is our Expect example script from the file ftp_drvr.exp.

proc ftp_login { host username password } {
  upvar spawn_id spawn_id
  spawn ftp $host
  send "$username\r"
  expect -re "331 Password required for $username" {
   expect -re "Password:" {
     send "$password\r"

This example demonstrates an Expect procedure -- remember it's Tcl syntax -- to login to a Unix-style FTP server. As mentioned, Expect provides the components for spawning a process. The spawned process ID is stored in Expect as an integer. The upvar command is not a typo. The spawn_id, as explained by Don Libes in his book, "Exploring Expect," has a scope local to the calling procedure. The Tcl upvar command pushes the scope up to a global variable named spawn_id.

The next item you'll notice in the script is the Expect command (not Tcl), expect. The expect command handles the interactions with the command prompts of the application, such as an FTP server login prompt. If the expect criteria is met, which is typically pattern matching, the command in braces is invoked. Here we see the user name and password submitted to the FTP server.

The driver script for this procedure is as follows,

set host "skynet"
set username "spongebob"
set password "ixnay123"

set spawn_id [ftp_login $host $username $password]

Another Tcl caveat: Note the open and close braces syntax is used when calling a Tcl procedure. To call this script, you can execute the following command-line statement.

Spongebob# expect ftp_example.exp

Did you notice we didn't use the tclsh interpreter? We used the Expect interpreter instead. A standard practice when using Expect scripts is to test the scripts in the interpreter environment and then make the script an executable. You can do this by adding the following command on the first line of your script.


Be sure and give the file the correct executable rights.

chmod 777 ftp_example.exp

Now try it out.


Now you're a system test hacker!

Tcl/Tk a fork in the road

Related Articles

Using the VI Editor

Scanning Your Network

Operating Network Services Under OpenBSD

Networking as a 2nd Language

Tk, the X11 toolkit for Tcl, has recently surfaced as ports for both Darwin and Mac OS X. The latter version surfaced on October 15, 2001, as a programming effort by Jim Ingham at Apple Computer. Tcl/Tk for Darwin is available from the Fink project. Fink is an installation tool for adding and removing packages to Darwin. The Fink utility requires a Unix-savvy user. At times Fink can be clunky and may require manual intervention. On the flip side, Jim Ingham's Tcl./Tk for Aqua package is easy to install, but is native only to the Mac OS X environment. Keep in mind that the Expect package is not compatible with either of these installations.

Tk for Aqua

Packages are being ported to Aqua and Darwin in droves. I have installed both the Fink Tcl/Tk and Mac OS X Tcl/Tk package, macOSXTk8.4a4.tar.gz. I found the latter package is the one I use the most. And it is also the easiest package to install. Packages I typically use, such as QuickTime for Tcl/Tk will be ported to Mac OS X soon.

Installation of Tcl/Tk for Aqua

Installation of Jim Ingham's package is simple. Download the file macOSXTk8.4a4.tar.gz and decompress it. Two parent folders are provided, one is Applications, for the, and the other parent folder is titled "Library". The Library folder contains the Frameworks folder. Open the Frameworks folder and select all the contents and drag them over to the ~/Library/Frameworks folder. You can drag the file over to the Applications folder if you wish. Now just double-click on and you're up and running.


Running under Mac OS X environment

Figure 5. Running under Mac OS X environment.

What now?

Admittedly, Tcl takes a lot of getting used to. At first, most people from a systems programming background tend to get a little frustrated. I know I was. After a couple of Tcl coding projects, my C and C++ instincts mellowed out. Just tackle a simple project first. The FTP code I provided would easily give you the self-inflicted frustration you desire. Just play around with the spawn_id. Once you conquer these obstacles, you'll be amazed at how easily you can hack out a Tcl or Expect script. You can manage an entire large system test network with Tcl.

As for Expect, I found it complements Perl rather nicely. An Expect script can launch and take statistics of your device under test. The results can be handed off in Perl for posting on an engineering web server. Wow, all this stuff on your Mac OS X system! And the doubters said there was no useful software out there for us yet ...

Michael J. Norton is a software engineer at Cisco Systems.

Return to the Mac DevCenter.