10-18-2024 08:46 AM - edited 10-18-2024 11:47 AM
I'm trying to run a tcl script (via automation or pasting into vty. The commands all individually work but if I try to push it all out, it stops feeding the commands after the first "if" conditional command.
I'm basically trying to rewrite a couple of interface descriptions based on whether there is a port that is line & protocol "up", as that interface is reserved for a FW connection. If 1/0/36 is up/up, rewrite a couple of descriptions. if not up/up, commands will be ignored and move on to the next set of commands.
tclsh
set swhostname [exec sh run | inc hostname]
regexp {(\w*)\-(\w\w)\-sw-A} $swhostname match location_id floor
set FWexist [exec sh int desc | inc Gi1/0/36]
regexp {Gi1\/0\/36\s+(up)\s+(up)} $FWexist match 36line 36protocol
if { $36line == "up" && $36protocol == "up" } {set fwname $location_id-$floor-FW-A}
## The above commands run and the first "if" provides the expected output, but nothing below runs.
if { $36line == "up" && $36protocol == "up" } {ios_config "int gig1/0/36" "description NTW|$fwname|wan1|V2"}
if { $36line == "up" && $36protocol == "up" } {ios_config "int gig1/0/37" "description NTW|$fwname|wan2|V200"}
#
set int35desc [exec sh run int gig1/0/35 | inc desc]
set int35upup [exec sh int desc | inc Gi1/0/35]
set int35trunk [exec sh int status | inc Gi1/0/35]
regexp {\w*\-\w\w\-FW-A} $int35desc fwnameconfirm
regexp {connected\s+(\w\w\w\w\w)} $int35trunk fw35trunk
if {$int35desc=="SW-FW/V1"} {set $fwnameconfirm}
regexp {Gi1\/0\/35\s+(up)\s+(up)} $int35upup match 35line 35protocol
##if {$fwnameconfirm == $fwname} {set fwlan1description "description NTW|$fwname|lan1|V1"}
##if {$fwnameconfirm == $fwname && $35line == "up" && $35protocol == "up"} {ios_config "int gig1/0/35" "description NTW|$fwname|lan1|V1"}
if {$fw35trunk == "trunk"&& $35line == "up" && $35protocol == "up"} {ios_config "int gig1/0/35" "description NTW|$fwname|lan1|V1"}
}
exec sho int desc | i FW
##more commands follow
tclquit
02-02-2025 11:04 PM
Hello @keith.bennett
The issue you're encountering is likely due to how the tclsh
interpreter processes the script and the commands.
When running a script in tclsh
, especially in a network device environment, there are a few things to keep in mind:
Command Execution Context: Some commands like ios_config
or exec
may not behave as expected if they are not properly formatted or if the environment doesn't support them in the way you're using them.
Error Handling: If any command in the script fails, the script may stop executing further commands. This is a common issue in network automation scripts.
Variable Scope and Syntax: Ensure that variables are properly defined and used. For example, set \$fwnameconfirm
is incorrect syntax. It should be set fwnameconfirm
.
Debugging: Add debugging statements to check the flow of the script and the values of variables at different points.
Here’s a revised version of your script with some corrections and debugging statements added:
tclsh # Get the hostname and extract location_id and floor set swhostname [exec sh run | inc hostname] regexp {(\w*)\-(\w\w)\-sw-A} \$swhostname match location_id floor
# Check if Gi1/0/36 is up/up set FWexist [exec sh int desc | inc Gi1/0/36] regexp {Gi1\/0\/36\s+(up)\s+(up)} \$FWexist match 36line 36protocol
# Debugging: Print the values of 36line and 36protocol puts "36line: \$36line, 36protocol: \$36protocol" if { \$36line == "up" && \$36protocol == "up" } { set fwname "\$location_id-\$floor-FW-A" puts "FW name: \$fwname" ios_config "int gig1/0/36" "description NTW|\$fwname|wan1|V2" ios_config "int gig1/0/37" "description NTW|\$fwname|wan2|V200" }
# Check interface Gi1/0/35 set int35desc [exec sh run int gig1/0/35 | inc desc] set int35upup [exec sh int desc | inc Gi1/0/35] set int35trunk [exec sh int status | inc Gi1/0/35]
# Debugging: Print the values of int35desc, int35upup, and int35trunk puts "int35desc: \$int35desc" puts "int35upup: \$int35upup" puts "int35trunk: \$int35trunk"
regexp {\w*\-\w\w\-FW-A} \$int35desc fwnameconfirm regexp {connected\s+(\w\w\w\w\w)} \$int35trunk fw35trunk regexp {Gi1\/0\/35\s+(up)\s+(up)} \$int35upup match 35line 35protocol
# Debugging: Print the values of fwnameconfirm, fw35trunk, 35line, and 35protocol puts "fwnameconfirm: \$fwnameconfirm" puts "fw35trunk: \$fw35trunk" puts "35line: \$35line, 35protocol: \$35protocol"
if {\$fw35trunk == "trunk" && \$35line == "up" && \$35protocol == "up"} { ios_config "int gig1/0/35" "description NTW|\$fwname|lan1|V1" }
# Final command to show interface descriptions exec sh int desc | inc FW tclquit |
|
Key Changes and Additions:
4. Flow Control: Ensured that the script continues execution even if some conditions are not met. Debugging Tips:
If the script still stops after the first
Hope This Helps!!! AshSe Forum Tips:
|
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide