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.
#!/usr/local/bin/expect
Be sure and give the file the correct executable rights.
chmod 777 ftp_example.exp
Now try it out.
./ftp_example.exp
Now you're a system test hacker!
Tcl/Tk a fork in the road
Related Articles |
|
Using the VI Editor |
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 wish.app, 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 wish.app file over to the Applications folder if you wish. Now just double-click on wish.app and you're up and running.
|
|
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.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 10 of 10.
-
invalid links
2003-08-26 11:43:45 anonymous2 [Reply | View]
-
Get Tk/Tcl and expect from Apple's site
2003-08-31 20:13:44 anonymous2 [Reply | View]
You can get the "batteries included" distribution of tcl/tk that installs expect with it. I have used the 8.4.2 version, I see this one is newer and just came out. Get it at Apple's site:
http://www.apple.com/downloads/macosx/unix_open_source/tcltkaqua.html
Also, you can get an autolearning script by going to expect.nist.gov and downloading the expect package. Don't install it, but keep the examples folder which has the autolearn script. It nakes a script after watching you perform a telnet or ftp session, etc. The FAQ on that site tells how to use it. So you can use expect even if you aren't a programmer.
-
Follow up article location
2003-02-20 06:57:42 anonymous2 [Reply | View]
The follow up article referenced can be found here: http://www.macdevcenter.com/pub/a/mac/2001/11/27/router_tcl.html
-
Where is the other article referenced?
2001-11-05 13:41:43 markstewart [Reply | View]
Cisco Router Management Using Tcl on Mac OS X??? Or if its not up yet, when is it due?
Mark -
Cisco Router Management
2003-06-14 14:59:08 anonymous2 [Reply | View]
If you mean a program to backup your Cisco router/switch configurations, there is a program called RANCID which runs fine on OS X. I installed the "batteries included" distribution of tcl/tk aqua which is an OS X image and it has the expect program included. That is the only pre-requisite for OS X to run RANCID. Then just follow the instructions on the RANCID site. Don't forget to edit the top line of the clogin expect script to be #! /usr/local/bin/expect.
http://www.shrubbery.net/rancid/
-
Updates to the prose ...
2001-10-30 10:59:18 hobbs [Reply | View]
I think this is a good article to describe how OS X will be all the more powerful as people take more advantage of the Unix base. As for the Tcl references, I wanted to make some small comments / corrections. The reference to http://www.scriptics.com/ for Tcl should be http://www.tcl-tk.net/ or http://tcl.sourceforge.net/. www.scriptics.com now is a direct DNS pointer to www.tcl-tk.net, but the tcl-tk reference is better in the long run.
I am curious why the author needed 8.4a2 to build Expect with (since the default tclsh on OS X is 8.3.x, IIRC). Expect, being a good Unix Tcl extension, should have built with the stock tclsh on the Mac.
The choice of ftp perhaps wasn't ideal to showcase Expect, since that can be done with the pure Tcl ftp module in tcllib http://tcllib.sourceforge.net/), but Expect is still a rather formidable extension. Now that the Mac has a Unix base, that opens up a lot of control of the rest of the world via Expect from the Mac.
The closing remarks about complementing Perl, which would be used on the web server, seem ... unimaginative to me. After all, Tcl can be a full web server, and tclhttpd (http://tclhttpd.sf.net/), written completely in Tcl, is reported to work just fine on OS X (that's the same web server running http://www.tcl-tk.net/).
Jeff Hobbs - Tcl Core Release Manager -
Updates to the prose ...
2001-10-30 11:57:38 mnorton01 [Reply | View]
hi Jeff,
Thanks for the feedback.
As for the 8.4a2 build that is the only release that appeared with Expect for the Macintosh. I agree, Expect should HAVE been included with the stock tclsh for the Mac. Apple comments???
Choosing the ftp example was merely to demonstrate console login and interaction. This was a simple example to show login and authentication for a UNIX style platform.
As for Perl, in large corporate environments remember coding is a collaboration. I may write the report tools in Tcl but I hand off the info to another person who maintains the web server. This admin may choose to code in Java or Pearl.
thanks,
Mike
-
Updates to the prose ...
2001-10-30 19:53:30 hobbs [Reply | View]
Mike,
It would be interesting and beneficial if expect could be part of the standard Mac OS X distribution.
Also, I do understand the reality of multi-language environments, especially in coporate environments. I just wanted to plug tclhttpd as well, for those that may be working on a smaller scale, or are looking for an all-inclusive approach for a project.
Thanks,
Jeff
-
incorrect regular expressions
2001-10-30 06:59:24 glennjnn [Reply | View]
The regular expression examples on page 3 of this
article do not match
% set myPhoneList "555-1212 nautilus.ipvoice.com"
% regexp { ([0-9]+)-([0-9]+) } $myPhoneList matchFound prefix localNumber
This does not match because there is no space
preceding 555 in $myPhoneList
The 800 example is similarly incorrect.
In the last example, the "Ethernet|Fddi" one,
the regular expression does not have a
closing brace}
Did you not test the examples first?
-
incorrect regular expressions
2001-10-30 11:38:53 mnorton01 [Reply | View]
hi,
Good catch. The braces with space padding must have been reformatted for the article. Yes,
these were tested. Somehow I must have mangled them when I cut and pasted into Word from tclsh. Here's the formatting bug scrub.
thanks,
Mike
Example nautilus.ipvoice.com
set myPhoneList "555-1212 nautilus.ipvoice.com"
regexp {([0-9]+)-([0-9]+)} $myPhoneList matchFound prefix localNumber
% set myPhoneList "555-1212 nautilus.ipvoice.com"
555-1212 nautilus.ipvoice.com
% regexp {([0-9]+)-([0-9]+)} $myPhoneList matchFound prefix localNumber
1
% puts $prefix
555
% puts $localNumber
1212
Example 800 Number
set myPhoneList "(800) 555-1212 nautilus.ipvoice.com"
regexp {(\([0-9]+\) )?([0-9]+)-([0-9]+)} \
$myPhoneList matchFound areaCode prefix localNumber
puts "$areaCode $prefix $localNumber"
% set myPhoneList "(800) 555-1212 nautilus.ipvoice.com"
(800) 555-1212 nautilus.ipvoice.com
% regexp {(\([0-9]+\) )?([0-9]+)-([0-9]+)} \
$myPhoneList matchFound areaCode prefix localNumber
1
% puts "$areaCode $prefix $localNumber"
(800) 555 1212
%
Example Get Interface Status
set myRouterInt "Ethernet1/0 121.1.1.2 YES manual up up"
regexp {(Ethernet|Fddi)([0-9])/([0-9]) .*(up|down)} \
$myRouterInt match int_type slot_id port_id int_status
% set myRouterInt "Ethernet1/0 121.1.1.2 YES manual up up"
Ethernet1/0 121.1.1.2 YES manual up up
% regexp {(Ethernet|Fddi)([0-9])/([0-9]) .*(up|down)} \
$myRouterInt match int_type slot_id port_id int_status
1
% put "$int_type $slot_id $port_id $int_status"
Ethernet 1 0 up







seem to be no longer valid.
I looked through both the Expect and Tcl sites and
couldn't find these versions for Mac OS X.
Where can these be found? Thanks in advance!