06-24-2015 12:43 PM
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.
Solved! Go to Solution.
06-27-2015 12:47 PM
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
}
06-27-2015 01:08 PM
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
06-27-2015 10:57 AM
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.
06-27-2015 12:47 PM
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
}
06-27-2015 01:08 PM
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
06-28-2015 07:15 AM
The easiest way to find the hostname of a local device in tclsh is:
set hostname [info hostname]
07-22-2015 01:29 PM
Thanks Joe!
07-27-2015 07:24 PM
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?
07-27-2015 09:07 PM
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
}
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