Can I combine EEM applets with Tcl shell?
When I’ve been describing the limitations of kron, someone quickly asked an interesting question:
As I cannot insert extra input keystrokes with EEM applet, can I run a Tcl script from it with the action sequence cli command “tclsh script” command and use the typeahead function call to get around the limitation?”
The only answer I could give at that time was “maybe” … and obviously it was time for a more thorough test. The short result is: YES, you can do it (at least in IOS release 12.4(15)T1).
I’ve started by creating a small Tcl script (see below) that clears the counters on Loopback 0. As the clear counters command requires keyboard input and generates a syslog message, it was a perfect test case.
typeahead "y"
exec "clear counter loop 0"
I’ve copied this script into the flash:tcl/clearL0.tcl and tested it:
R1#tclsh flash:tcl/clearL0.tcl
%CLEAR-5-COUNTERS: Clear counter on interface Loopback0 by console
So far, so good. Next, I’ve created an EEM applet with no trigger:
event manager applet Clear
event none
action 1.0 cli command "enable"
action 1.1 cli command "tclsh flash:tcl/clearL0.tcl"
… enabled the EEM CLI debugging and started it:
R1#debug event man action cli
Debug EEM action cli debugging is on
R1#event man run Clear
R1#
%HA_EM-6-LOG: Clear : DEBUG(cli_lib) : : CTL : cli_open called.
%HA_EM-6-LOG: Clear : DEBUG(cli_lib) : : OUT :
%HA_EM-6-LOG: Clear : DEBUG(cli_lib) : : OUT : R1>
%HA_EM-6-LOG: Clear : DEBUG(cli_lib) : : IN : R1>enable
%HA_EM-6-LOG: Clear : DEBUG(cli_lib) : : OUT :
%HA_EM-6-LOG: Clear : DEBUG(cli_lib) : : OUT : R1#
%HA_EM-6-LOG: Clear : DEBUG(cli_lib) : : IN : R1#tclsh flash:tcl/clearL0.tcl
%CLEAR-5-COUNTERS: Clear counter on interface Loopback0 by on vty0 (EEM:Clear)
%HA_EM-6-LOG: Clear : DEBUG(cli_lib) : : OUT :
%HA_EM-6-LOG: Clear : DEBUG(cli_lib) : : OUT : R1#
%HA_EM-6-LOG: Clear : DEBUG(cli_lib) : : CTL : cli_close called.
Great. It works. Let’s move on: I’ve inserted a trigger into the EEM applet that ran the applet when an OSPF neighbor reached FULL adjacency:
event manager applet Clear
event syslog pattern "OSPF-5-ADJCHG.*to FULL"
action 1.0 cli command "enable"
action 1.1 cli command "tclsh flash:tcl/clearL0.tcl"
And now for the final test: after I’ve enabled the serial interface, OSPF neighbors established adjacency …
R1(config-if)#interface ser 1/0
R1(config-if)#no shutdown
R1(config-if)#
%LINK-3-UPDOWN: Interface Serial1/0, changed state to up
%LINEPROTO-5-UPDOWN: Line protocol on Interface Serial1/0, changed state to up
%OSPF-5-ADJCHG: Process 1, Nbr 10.0.1.2 on Serial1/0 from LOADING to FULL, Loading Done
%CLEAR-5-COUNTERS: Clear counter on interface Loopback0 by on vty0 (EEM:Clear)
… and the counters on Loopback0 were cleared. Mission accomplished :)
Information about EEM is very interesting. We try to use it for monitoring of multicast traffic.
action X cli command "clear counters" pattern "confirm"
action X+1 cli command "y"
?
I'm using Dynagen to learn the EEM. Don't want to connect the Dynagen routers to my network, and accidentally have one of my test labs affect the network.
For a quick short Tcl script it would be convenient to be able to create the script from the router CLI.
As for the other part of your question, search the forums about the use of loopback interface.
I would like to put a question as well.
I can combine eem applets with tcl scripts. But, if I want to start the tcl script based on the syslog msg?
How can I get the syslog message into the script?
Thanks,
Gustavo
If you really do want to pass the syslog message to a "pure" Tcl script, the tclsh accepts arguments after the filename so you may be able to pass something in there from the applet. For example, applets that were triggered by the syslog event detector will have the $_syslog_msg variable set. So you could use that variable in the applet. I have never tried it, but it might be possible.
I´ve run into a couple more problems, though, and I would appreciate some more help...
when i use cli_open to get a command-line interface i get the following error:
cannot get pty for exec: Error opening vty no tty lines available, minimum of 2 required by EEM
do you know how i can get past this?
Also, can anyone tell me how to increase the time limit for script execution? the default value is 20 seconds and I would like to increase it
To change the default timeout, use the maxrun parameter in the event_register_xxx line in your Tcl policy.
It works with the maxrun option! and.. with your help I just fixed some mistakes in my script :D
I´m trying to find how can I copy (append) a file to a ftp.
The end of my script will create a file into flash with the output. I would like to copy that file to ftp. With the command copy flash:filename ftp://.... it will overwritte. I just want to append...
Do you know how to do that?
Thanks a lot for the help!
event manager applet t
event none
action 1.0 cli command "enable"
action 2.0 cli command "tclsh"
action 2.1 cli command "typeahead \"y\" ; exec \"clear counter\""
action 3.0 cli command "exit"
Can anyone verify the syntax? I can get it to work on the console, but not in eem
I have an issue with trying to create a script that will handle the "yes/no" question that arises during the shutting down of a active Transcoder.
What I need to perform via a TCL script is:
conf t
dspfarm profile 8 trancode
shutdown
exit
exit
Of course a typeahead "yes\n" doesn't seem to help due, perhaps due to the fact that the ios_config uses a separate VTY context to issue the its commands.
I have tried to use the EEM with no success:
event manager applet disableProf
event none
action 1.0 cli command "conf t"
action 1.1 cli command "dspfarm profile 8 transcode"
action 1.2 cli command "yes\n"
action 1.3 cli command "exit"
action 1.4 cli command "exit"
But the transcoder profile remains active.
It seems user prompt questions within the processing via the "exec" construct seems to be possible to handle, however does anyone know of a way to programmatically answer prompted questions with the "ios_config" construct ?
If the answer is no, then can anyone think of other ways to shutdown a transcoder profile ? (could it be achieved via TCL via the SNMP accessing functionality?
Here is the actually prompt text for those who haven’t seen a prompted question within the conf t prompt:
Router(tcl)#ios_config "conf t" "dspfarm profile 8 transcode" "shutdown" "exit"
Disabling profile will disconnect active TRANSCODING calls,
do you want to continue ? [yes/no]
Must be yes or no
Thanks for all that read this.
Thanks to Ivan for his excellent blog space.
http://blog.ioshints.info/2007/12/execute-cli-commands-with-prompts-in.html
Good luck!
Ivan