The documentation set for this product strives to use bias-free language. For the purposes of this documentation set, bias-free is defined as language that does not imply discrimination based on age, disability, gender, racial identity, ethnic identity, sexual orientation, socioeconomic status, and intersectionality. Exceptions may be present in the documentation due to language that is hardcoded in the user interfaces of the product software, language used based on RFP documentation, or language that is used by a referenced third-party product. Learn more about how Cisco is using Inclusive Language.
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 Cisco NX-OS devices.
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 above example, the Cisco NX-OS command help function is still available but the Tcl puts command returns an error from the help function. |
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. |
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.
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 will 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 will fail and the Tcl shell will display an error:
show module $x | incl Mod "show module $x | incl Mod"
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 will 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)#
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)#
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 will terminate the Tcl shell only from the EXEC command mode.
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.
You can run Tcl commands from either a script or on the command line using the tclsh command.
Note | You cannot create a Tcl script file at the CLI prompt. You can create the script file on a remote device and copy it to the bootflash: directory on the Cisco NX-OS device. |
1.
tclsh [bootflash:filename [argument ...
]]
The following example shows an interactive Tcl shell:
switch# tclsh switch-tcl# set x 1 switch-tcl# cli show module $x | incl Mod Mod Ports Module-Type Model Status 1 36 36p 40G Ethernet Module N9k-X9636PQ ok Mod Sw Hw Mod MAC-Address(es) Serial-Num switch-tcl# exit switch#
The following example shows how to run a Tcl script:
switch# show file bootflash:showmodule.tcl set x 1 while {$x < 19} { cli show module $x | incl Mod set x [expr {$x + 1}] } switch# tclsh bootflash:showmodule.tcl Mod Ports Module-Type Model Status 1 36 36p 40G Ethernet Module N9k-X9636PQ ok Mod Sw Hw Mod MAC-Address(es) Serial-Num switch#
You can change modes in Cisco NX-OS while you are running an interactive Tcl shell.
1.
tclsh
2.
configure terminal
3.
tclquit
The following example shows how to change Cisco NX-OS modes from an interactive Tcl shell:
switch# tclsh 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 Enter description of maximum 80 characters inherit Inherit a port-profile ip Configure IP features ipv6 Configure IPv6 features logging Configure logging for interface no Negate a command or set its defaults rate-limit Set packet per second rate limit shutdown Enable/disable an interface this Shows info about current object (mode's instance) vrf Configure VRF parameters end Go to exec mode exit Exit from command interpreter pop Pop mode from stack or restore from name push Push current mode to stack or save it under name where Shows the cli context you are in switch(config-if-tcl)# description loop10 switch(config-if-tcl)# tclquit Exiting Tcl switch#
The following titles are provided for your reference:
Mark Harrison (ed), Tcl/Tk Tools, O'Reilly Media, ISBN 1-56592-218-2, 1997
Mark Harrison and Michael McLennan, Effective Tcl/Tk Programming, Addison-Wesley, Reading, MA, USA, ISBN 0-201-63474-0, 1998
John K. Ousterhout, Tcl and the Tk Toolkit, Addison-Wesley, Reading, MA, USA, ISBN 0-201-63337-X, 1994.
Brent B. Welch, Practical Programming in Tcl and Tk, Prentice Hall, Upper Saddle River, NJ, USA, ISBN 0-13-038560-3, 2003.
J Adrian Zimmer, Tcl/Tk for Programmers, IEEE Computer Society, distributed by John Wiley and Sons, ISBN 0-8186-8515-8, 1998.