10-11-2013 02:23 AM
I'm quite confuse by this error???
Basically I'm exploring the options fo the cli_ command extensions in Tcl. So I wrote quite a simple script:
Terminal#more flash:/cli_alternate.tcl
::cisco::eem::event_register_none
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
if {[catch {cli_open} result]} {
puts stderr $result
exit 1
} else {
action_syslog priority emergencies msg "EEM:Opening a vty line."
array set cli1 $result
}
set cmd_exec "clear line 46"
set read_pattern "[continue]"
set cmd_response "y"
if {[catch {cli_write $cli1(fd) "enable"} result]} {
puts stderr $result
exit 1
}
if {[catch {cli_write $cli1(fd) $cmd_exec} result]} {
puts stderr $result
exit 1
}
if {[catch {cli_read_pattern $cli1(fd) $read_pattern} result]} {
puts stderr $result
exit 1
}
if {[catch {cli_write $cli1(fd) $cmd_response} result]} {
puts stderr $result
exit 1
}
if {[catch {cli_close $cli1(fd) $cli1(tty_id)} result]} {
puts "Warning: $result"
}
return -code ok
I register the script, no error. Once I try to start it:
Terminal#event manager run cli_alternate.tcl
invoked "continue" outside of a loop
while executing
"eval_script slave $scriptname"
invoked from within
"if {$security_level == 1} { #untrusted script
interp create -safe slave
interp share {} stdin slave
interp share {} stdout slave
..."
(file "tmpsys:/lib/tcl/base.tcl" line 50)
Tcl policy execute failed: invoked "continue" outside of a loop
My understanding is that an internal function is called and it outputs an error. But why? I've checked my syntax a dozen times, and it should be in order, otherwise the system will refuse to register the script... What's wrong here? In writing the script I borrowed heavily from the embeded command extension cli_run_interactive from more tmpsys:/lib/tcl/cli_lib.tcl.
Solved! Go to Solution.
10-11-2013 08:45 AM
Two errors I see. One, anytime you use [], you have to make sure you either escape '[' or you enclose that string in {}. So, the following needs to be changed:
set read_pattern "[continue]"
To:
set read_pattern {[continue]}
Next, you have "else{" above. You need a space between else and '{'. If you use the following code, it will work:
if {[catch {cli_exec $cli1(fd) "show interface FastEthernet0/0"} result]} {
puts stderr $result
exit 1
} else {
set cmd_output $result
puts $result
}
10-11-2013 02:48 AM
There's something terribly wrong here... Just for a test, I wrote an even simpler script:
::cisco::eem::event_register_none
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
if {[catch {cli_open} result]} {
puts stderr $result
exit 1
} else {
action_syslog priority emergencies msg "EEM:Opening a vty line."
array set cli1 $result
}
if {[catch {cli_exec $cli1(fd) "show interface FastEthernet0/0"} result]} {
puts stderr $result
exit 1
} else{
set cmd_output $result
puts $result
}
if {[catch {cli_close $cli1(fd) $cli1(tty_id)} result]} {
puts "Warning: $result"
}
return -code ok
And yet again, I'm getting the same error when I try to execute it.....
Terminal#event manager run cli_show.tcl
invoked "continue" outside of a loop
while executing
"eval_script slave $scriptname"
invoked from within
"if {$security_level == 1} { #untrusted script
interp create -safe slave
interp share {} stdin slave
interp share {} stdout slave
..."
(file "tmpsys:/lib/tcl/base.tcl" line 50)
Tcl policy execute failed: invoked "continue" outside of a loop
So it seems there's something conceptually wrong with my scripts in general
10-11-2013 03:03 AM
It's getting even more confusing... I was able to isolate the culpring for the error, but... still I can not understand, why it is happening.
So, by replacing
if {[catch {cli_exec $cli1(fd) "show interface FastEthernet0/0"} result]} {
puts stderr $result
exit 1
} else{
set cmd_output $result
puts $result
}
with the follwoing:
cli_exec $cli1(fd) "show interface FastEthernet0/0"
set cmd_output $result
puts $cmd_output
I'm able to execute the script without an error. But why? From all the examples out there, I've seen that most people prefer to use the "if" statement for all of the cli_ commands, because it allows to have a more precise debugging in case the an error is encountered... But why it's failing?
10-11-2013 08:45 AM
Two errors I see. One, anytime you use [], you have to make sure you either escape '[' or you enclose that string in {}. So, the following needs to be changed:
set read_pattern "[continue]"
To:
set read_pattern {[continue]}
Next, you have "else{" above. You need a space between else and '{'. If you use the following code, it will work:
if {[catch {cli_exec $cli1(fd) "show interface FastEthernet0/0"} result]} {
puts stderr $result
exit 1
} else {
set cmd_output $result
puts $result
}
10-17-2013 02:55 AM
Hi Joseph,
You're absolutelly correct on that. It's clear that my Tcl writing skill do require some refinment
I tested both of your remarks today and I confirm that both scripts now work. I notice something strange while executing the script cli_alternate.tcl, The log says that it has executed as expected, but still I think there's some issue there. I'll need some time to examine it and will get back to this thread. At least, thanks to you, now I'm sure that the syntax is correct Thank you.!
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