cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
5035
Views
5
Helpful
7
Replies

Help: TCL Syntax for redirecting output to a file

rpeasah
Beginner
Beginner

I want to grab some outputs from some show commands and redirect the output to a file named after each router and I'm stuck. Here's shortened version of my "crude" script:

tclsh
set FINDROUTERNAME [exec "show running-config | i ^hostname"]
set ROUTERNAME [lindex [split $FINDROUTERNAME " "] 1]
ios_config "file prompt quiet" "end"
exec ["show ip int brief | redirect flash:$ROUTERNAME.txt"]
exec ["show ip route | append flash:/$ROUTERNAME.txt"]
exec ["show bgp vpnv4 uni all summ | append flash:/$ROUTERNAME.txt"]
exec ["show running-config | append flash:/$ROUTERNAME.txt"]
tclquit

 

The output redirection is not working:

NOTE The error symbol is actually at the flash command.

R1#tclsh
R1(tcl)#set FINDROUTERNAME [exec "show running-config | i ^hostname"]
hostname R1

R1(tcl)#set ROUTERNAME [lindex [split $FINDROUTERNAME " "] 1]
R1

R1(tcl)#ios_config "file prompt quiet" "end"
R1(tcl)#exec ["show ip int brief | redirect flash:$ROUTERNAME.txt"]
invalid command name "show ip int brief | redirect flash:R1
.txt"                                               ^
% Invalid input detected at '^' marker.

 

2 Accepted Solutions

Accepted Solutions

Add these lines at #additional cli and append statements in the original script.

set IP [list 10.1.1.1 10.2.2.2]
foreach IP $IP {
set result [exec ping $IP]
append contents $result
}

 

View solution in original post

Dan Frey
Cisco Employee
Cisco Employee

Would be best to make this an EEM TCL policy  ... however this may work.

set result [exec dir | inc Directory]
regexp -nocase [subst -nocommands -nobackslashes {Directory of ([a-zA-Z0-9]+)}] $result match media

set FINDROUTERNAME [exec "show running-config | i ^hostname"]
regexp {hostname ([^\s]+)} $FINDROUTERNAME match ROUTERNAME

 if {! [file exists $media:$ROUTERNAME]} {
    set file [open "$media:$ROUTERNAME" "w"]
    close $file
  }


set file [open "$media:$ROUTERNAME" "r"]
set contents [read $file]
close $file

set result [exec sh ip route]
append contents $result
set result [exec sh ip int brief]
append contents $result
#.... additional cli and append statements here

set file [open "$media:$ROUTERNAME" "w"]
puts $file $contents
close $file

 

View solution in original post

7 Replies 7

rpeasah
Beginner
Beginner

Thanks Dan. Would like to add one more thing, which is to add the output of ping tests like:

foreach $IP {

10.1.1.1

10.2.2.2

} {ping $IP}

Can't figure out how to add the ping results to the "result" variable in your solution.

Add these lines at #additional cli and append statements in the original script.

set IP [list 10.1.1.1 10.2.2.2]
foreach IP $IP {
set result [exec ping $IP]
append contents $result
}

 

Dan Frey
Cisco Employee
Cisco Employee

Would be best to make this an EEM TCL policy  ... however this may work.

set result [exec dir | inc Directory]
regexp -nocase [subst -nocommands -nobackslashes {Directory of ([a-zA-Z0-9]+)}] $result match media

set FINDROUTERNAME [exec "show running-config | i ^hostname"]
regexp {hostname ([^\s]+)} $FINDROUTERNAME match ROUTERNAME

 if {! [file exists $media:$ROUTERNAME]} {
    set file [open "$media:$ROUTERNAME" "w"]
    close $file
  }


set file [open "$media:$ROUTERNAME" "r"]
set contents [read $file]
close $file

set result [exec sh ip route]
append contents $result
set result [exec sh ip int brief]
append contents $result
#.... additional cli and append statements here

set file [open "$media:$ROUTERNAME" "w"]
puts $file $contents
close $file

 

Joe Clarke
Hall of Fame Cisco Employee Hall of Fame Cisco Employee
Hall of Fame Cisco Employee

The easiest way to find the hostname of a local device in tclsh is:

 

set hostname [info hostname]

Thanks Joe!

how do you keep this script from failing due to ping failures? If you were to do this:

set result [exec ping 1.1.1.1 repeat 1000]

If the pings timeout the script fails. Is there a way for the script to continue executing subsequent tasks following the ping timeouts?

Joe Clarke
Hall of Fame Cisco Employee Hall of Fame Cisco Employee
Hall of Fame Cisco Employee

You can wrap any command in catch to prevent an error from being fatal:

 

if { [catch {exec ping 1.1.1.1 repeat 1000} result] } {

    # error here

}

 

 

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community:

Recognize Your Peers