cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
317
Views
0
Helpful
1
Replies

TCL if conditions ends a script remaining in tclsh

keith.bennett
Level 1
Level 1

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

1 Reply 1

AshSe
VIP
VIP

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:

  1. 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.

  2. 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.

  3. Variable Scope and Syntax: Ensure that variables are properly defined and used. For example, set \$fwnameconfirm is incorrect syntax. It should be set fwnameconfirm.

  4. 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:

  1. Debugging Statements: Added puts commands to print the values of variables at different points in the script. This will help you understand where the script might be failing.

  2. Corrected Syntax: Fixed set \$fwnameconfirm to set fwnameconfirm.

  3. Error Handling: If any command fails, the script will stop. To handle this, you can wrap commands in catch to prevent the script from stopping. For example:

if {[catch {ios_config "int gig1/0/36" "description NTW|\$fwname|wan1|V2"} result]} {

          puts "Error configuring interface Gi1/0/36: \$result"

}

      4. Flow Control: Ensured that the script continues execution even if some conditions are not met.

Debugging Tips:

  1. Run the script step by step in the tclsh environment to identify where it stops.
  2. Use puts to print variable values and ensure they are as expected.
  3. Check the output of exec commands to ensure they return the expected results.

If the script still stops after the first if block, it could be due to an issue with the ios_configcommand or the environment in which the script is running. In that case, try running the commands manually to verify their behavior.

 

Hope This Helps!!!

AshSe

Forum Tips: 

  1. Insert photos/images inline - don't attach.
  2. Always mark helpful and correct answers, it helps others find what they need.
  3. For a prompt reply, kindly tag @name. An email will be automatically sent to the member.

 

Review Cisco Networking for a $25 gift card