About Tcl
Tcl (pronounced "tickle") is a scripting language that increases flexibility of CLI commands. You can use Tcl to extract certain values in the output of a show command, perform switch configurations, run Cisco NX-OS commands in a loop, or define Embedded Event Manager (EEM) policies in a script.
This section describes how to run Tcl scripts or run Tcl interactively on switches.
Tclsh Command Help
Command help is not available for Tcl commands. You can still access the help functions of Cisco NX-OS commands from within an interactive Tcl shell.
This example shows the lack of Tcl command help in an interactive Tcl shell:
switch# tclsh
switch-tcl# set x 1
switch-tcl# puts ?
^
% Invalid command at '^' marker.
switch-tcl# configure ?
<CR>
session Configure the system in a session
terminal Configure the system from terminal input
switch-tcl#
Note |
In the preceding example, the Cisco NX-OS command help function is still available but the Tcl puts command returns an error from the help function. |
Tclsh Command History
You can use the arrow keys on your terminal to access commands you previously entered in the interactive Tcl shell.
Note |
The tclsh command history is not saved when you exit the interactive Tcl shell. |
Tclsh Tab Completion
You can use tab completion for Cisco NX-OS commands when you are running an interactive Tcl shell. Tab completion is not available for Tcl commands.
Tclsh CLI Command
Although you can directly access Cisco NX-OS commands from within an interactive Tcl shell, you can only execute Cisco NX-OS commands in a Tcl script if they are prepended with the Tcl cli command.
In an interactive Tcl shell, the following commands are identical and execute properly:
switch-tcl# cli show module 1 | incl Mod
switch-tcl# cli "show module 1 | incl Mod"
switch-tcl# show module 1 | incl Mod
In a Tcl script, you must prepend Cisco NX-OS commands with the Tcl cli command as shown in the following example:
set x 1
cli show module $x | incl Mod
cli "show module $x | incl Mod"
If you use the following commands in your script, the script fails and the Tcl shell displays an error:
show module $x | incl Mod
"show module $x | incl Mod"
Tclsh Command Separation
The semicolon (; ) is the command separator in both Cisco NX-OS and Tcl. To execute multiple Cisco NX-OS commands in a Tcl command, you must enclose the Cisco NX-OS commands in quotes ("" ).
In an interactive Tcl shell, the following commands are identical and execute properly:
switch-tcl# cli "configure terminal ; interface loopback 10 ; description loop10"
switch-tcl# cli configure terminal ; cli interface loopback 10 ; cli description loop10
switch-tcl# cli configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
switch(config-tcl)# cli interface loopback 10
switch(config-if-tcl)# cli description loop10
switch(config-if-tcl)#
In an interactive Tcl shell, you can also execute Cisco NX-OS commands directly without prepending the Tcl cli command:
switch-tcl# configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
switch(config-tcl)# interface loopback 10
switch(config-if-tcl)# description loop10
switch(config-if-tcl)#
Tcl Variables
You can use Tcl variables as arguments to the Cisco NX-OS commands. You can also pass arguments into Tcl scripts. Tcl variables are not persistent.
The following example shows how to use a Tcl variable as an argument to a Cisco NX-OS command:
switch# tclsh
switch-tcl# set x loop10
switch-tcl# cli "configure terminal ; interface loopback 10 ; description $x"
switch(config-if-tcl)#
Tclquit
The tclquit command exits the Tcl shell regardless of which Cisco NX-OS command mode is currently active. You can also press Ctrl-C to exit the Tcl shell. The exit and end commands change Cisco NX-OS command modes. The exit command terminates the Tcl shell only from the EXEC command mode.
Tclsh Security
The Tcl shell is executed in a sandbox to prevent unauthorized access to certain parts of the Cisco NX-OS system. The system monitors CPU, memory, and file system resources being used by the Tcl shell to detect events such as infinite loops, excessive memory utilization, and so on.
You configure the initial Tcl environment with the scripting tcl init init-file command.
You can define the looping limits for the Tcl environment with the scripting tcl recursion-limit iterations command. The default recursion limit is 1000 iterations.