oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

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

Tcl and regular expressions

One of the true powers of Tcl, which often becomes a love-hate relationship, is the regular expression (regexp) command. This command provides the flexibility of unique pattern searching. For a simple example, let's say you're testing a voice gateway server and you need to locate a phone number, 555-1212, to match to an IP address list. You could use:

% set myPhoneList "555-1212"
% regexp {([0-9]+)-([0-9]+)}  
   $myPhoneList matchFound prefix localNumber

Wow! What is that?

Welcome to the world of regular expressions. The regexp command has its arguments enclosed in curly braces. The funky ([0-9]+)-([0-9]+) is instructing regexp to grab all numbers with the pattern nnnnnnn-nnnnnn (hyphen-separated). The regexp command would identify that "555-1212" has this pattern and copy it into the variable matchFound.

% puts $matchFound

We instructed regexp to search for two subpatterns. The actual pattern was hyphen delimited. So, we can grab the two subpatterns and place these into sub-variables.

% puts $prefix
 % puts $localNumber

Okay, so what if we used an 800 number? What would our regexp look like? This is tricky. Let's take a look.

% set myPhoneList "(800) 555-1212"
% regexp {(\([0-9]+\))?([0-9]+)-([0-9]+)} \ 
   $myPhoneList matchFound  areaCode prefix localNumber

You'll notice we had to escape the open and close parentheses, "(" and ")" because they are a part of the regexp syntax.

Now for a real-life application, how about using a regexp command on a Cisco router interface. The following data is from an IOS "show interfaces" command and is in the variable myRouterInt.

Ethernet1/0      YES manual up        up      

We could use:

regexp {(Ethernet|Fddi)([0-9])/([0-9]) .*(up|down) \
  $myRouterInt match int_type slot_id port_id int_status

This little snippet could tell us if an interface is up or down. We'll learn more about poking around inside a Cisco router in the complementary article, "Cisco Router Management Using Tcl on Mac OS X." Examining the above little piece of code reveals a wealth of Tcl regexp coding practices. Such as, how to grab subpatterns from an input string and split the subpattern matches into sub-variables. Regular expressions are tough to master. So don't feel too bad if they look a bit complicated at first glance.


Related Reading

Tcl/Tk in a NutshellTcl/Tk in a Nutshell
By Paul Raines & Jeff Tranter
Table of Contents
Sample Chapter
Full Description

Even though Tcl is a powerful scripting language, it has its shortcomings. For instance, from a Unix environment I may want to FTP to another machine and retrieve a file. In Tcl, I could use the socket library and write an FTP script from the socket layer up. The other option is to have available a Tcl-based toolkit that handles interaction and spawning of processes. The toolkit with these goodies is our friend Expect, coded by Don Libes in 1990.

Expect provides all the hooks a system test engineer could desire. Most network devices provide either a console interface, an HTTP server, or both.

For example, Expect allows you to exercise a device to test a console parser. To exercise a device, one would use a script that would simulate typical or even stressful operations. A network device shouldn't crash because someone was using your device's console interface. Expect can exercise a console rather nicely. Another application is generating network traffic. Perhaps you wish to generate background traffic, such as file transfers, and then check the files for integrity after the transfer. Expect can also handle e-mailing out the reports after your script has completed. That ought to put a smile on your manager's face.

Pages: 1, 2, 3, 4

Next Pagearrow