The switch command is a powerful conditional command in the Tcl scripting language.
This command evaluates a pattern match on a string and runs the associated commands.
The default variable can be used to run a command set in the event that there was not a specific string match.
The syntax for the switch command is as follows:
switch ?switches? string pattern body ... ?default body?
The ?switches? parameter consists of the following options:
■ -exact: Compares the exact string pattern and is the default match pattern used.
■ -glob: Matches filenames.
■ -regexp: Matches regular expression match criteria.
The following example sets the value of drops to 1 and uses the switch command to eval-uate the condition:
Router(tcl)#set drops 1
Using the switch command, the evaluation matches 1 and the put command displays a 1:
Router(tcl)#switch $drops {
”0” { puts “0”
}
“1” { puts “1”
} default {
puts “default”
} } 1
Any value other than a 0 or 1 will cause the default section to execute. It is a good prac-tice to always include the default in all case statements, because if no matching value is found, an empty string is returned.
In Tcl, no break statement is needed, and each case will not result in a fall through as it does in the C programming language.
Files
Opening a file inside a program, or script in this case, requires much more attention to detail! Not only do you have to open the file for reading/writing, but you need to make sure that you close the file when you are finished with it; otherwise, you will waste resources. In addition, when a file is open, you have to know the location of the pointer to either read from or write to the contents.
The following example takes you through several commands in listing files, creating a file, writing information to that file, reading the contents, moving the pointer, closing the file, and finally deleting the file.
The first example begins by getting a list of the files that currently exist in the flash: file system of the router. You can do so using the dir command, which provides a list of files in the specified directory:
Router(tcl)#dir flash:
Directory of flash:/
1 -rw- 31261340 Dec 31 1983 00:01:00 +00:00 c2800nm-ipbasek9-mz.124-24.T1.bin
2 -rw- 3660 Jul 21 2009 20:28:04 +00:00 interface_errors_email.tcl
Note The dir command is not a Tcl command. This is an example where the Tcl inter-preter does not recognize the command and so sends it to the Cisco IOS exec-mode parser.
The output indicates that there are two files: the IOS image and a file called
interface_errors_email.tcl. Notice on the left the letters rw. This indicates that the files can be opened for reading or writing. The file size is also shown, along with the creation date.
Next, create a file called IntStats.dat, which will be used to store information about (you guessed it) interface statistics. When opening a file, you have several options, including the following:
■ r: Open for reading
■ r+: Open for reading and writing
■ w: Open for writing, overwrite existing file, or create a new one
■ w+: Open for reading and writing, overwrite existing file, or create a new one
■ a: Open for writing and append to file
■ a+: Open for reading and writing and append to file
The following example creates a file called IntStats.dat for writing and declares a variable called IntStats to reference the open file handle:
Router(tcl)#set IntStats [open IntStats.dat w]
file7
Chapter 2: Tcl Interpreter and Language Basics 29
Using information from the example on arrays, you will populate the file:
Router(tcl)#puts $IntStats “GigabitEthernet0/0 {382212 5133 125233}”
Router(tcl)#puts $IntStats “GigabitEthernet0/1 {382212 5133 125233}”
Router(tcl)#puts $IntStats “Serial0/0/0 {336373 383 27383}”
Notice that the puts command is used to send information to the file rather than to the terminal.
To close the file, enter the following:
Router(tcl)#close $IntStats
To verify that the file has been created, enter the following:
Router(tcl)#dir flash:
Directory of flash:/
1 -rw- 31261340 Dec 31 1983 00:01:00 +00:00 c2800nm-ipbasek9-mz.124-24.T1.bin
2 -rw- 3660 Jul 21 2009 20:28:04 +00:00 interface_errors_email.tcl
4 -rw- 111 Jul 28 2009 04:02:42 +00:00 IntStats.dat 31885312 bytes total (616448 bytes free)
The file IntStats is in the flash: directory with a file size of 111 bytes.
To open the file you just created for reading only, use the r option:
Router(tcl)#set IntStats [open IntStats.dat r]
file7
Now you can get some data from the open file using the gets command as demonstrated in the following example, and place that information in the variable data:
Router(tcl)#gets $IntStats data 39
What in the world is 39? That is not the data that you stored! The number 39 indicates where the pointer resides in the file. Look at the collected data using the puts command:
Router(tcl)#puts $data
GigabitEthernet0/0 {382212 5133 125233}
As you can see from the output, you have just the first line of data. If you count the number of characters starting from the left (do not forget to start with 0), you will see that you collected 38 characters plus newline (\n).
You can also determine where the pointer is by using the tell command as follows:
Router(tcl)#tell $IntStats 40
Now that you know the pointer is on the first character of the second line, you can gather some additional information using the read command. In this case, get the next 18 bytes:
Router(tcl)#read $IntStats 18 GigabitEthernet0/1
No surprise here: The output is GigabitEthernet0/1, as expected.
With the read command, you can read the rest of the file, as follows:
Router(tcl)#read $IntStats {382212 5133 125233}
GigabitEthernet0/1 {382212 5133 125233}
Now when you check to see where the pointer is, you can see that it is at the end of the file (EOF):
Router(tcl)#tell $IntStats 111
The file pointer can be moved to any location using the seek command. To direct the pointer to the beginning of the file, use the following command statement:
Router(tcl)#seek $IntStats 0
Note Not all Cisco IOS File System (IFS) devices support the seek operation. Some older flash types might not be supported.
You can use other methods covered earlier in the chapter to manipulate or search through the file for specific information.
When you have finished pulling information from the file, you should close it. Otherwise, you are wasting valuable memory resources. To close the file, simply use the following command:
Router(tcl)#close $IntStats
Finally, when you need to delete file, use the following command (with extreme caution):
Router(tcl)#file delete IntStats.dat
This should really be said for any of the file commands. You could inadvertently open or overwrite the IOS image or other files located on the flash, so always use care when manipulating files. In addition, files can be opened on remote devices. For example, you can open a file on a TFTP server using the following command:
Router(tcl)#open tftp://192.168.0.182/file.dat “w”
Chapter 2: Tcl Interpreter and Language Basics 31
Summary
One of the best ways to familiarize yourself with the commands and procedures discussed in this chapter is hands-on practice. If you do not have access to a router, you can perform most of what you previously read about on your own personal computer. Just install the appropri-ate Tcl shell software if it is not already there.
One last note of caution: When cutting and pasting information into a Tcl shell, you might be challenged with formatting errors. For example, quotes might not paste properly and may cause some interesting issues.
References
Tcl Command Reference: http://www.tcl.tk/man/tcl8.3/TclCmd/contents.htm