Cisco transfer-on-commit EEM applet

As a follow-up on my post about the Cisco configuration replace, rollback and commit feature; Tom Laermans suggested a Cisco implementation for Juniper’s JunOS its transfer-on-commit feature.

This is not a native feature of Cisco IOS; however, you can use my transfer-on-commit.tcl EEM applet to achieve the same result. Just upload this script to your Cisco IOS router, or put it on a location such as HTTP or TFTP. I strongly suggest to use the local storage of the router to reduce overhead.

Once you have uploaded the applet, use the EEM event manager to apply the applet. Please bear in mind that the user policy directory might be a different on your routing equipment!

br0.lab3#config t
br0.lab3(config)#event manager directory user policy disk0:/
br0.lab3(config)#event manager policy transfer-on-commit.tcl
br0.lab3(config)#end

br0.lab3#sh run | sect event manager
event manager directory user policy disk0:/
event manager policy transfer-on-commit.tcl
br0.lab3#

After applying the EEM policy, you can use the following verification commands for OAM (Operations, Administration, and Maintenance)

br0.lab3#sh event manager policy registered
No.  Class   Type    Event Type          Trap  Time Registered           Name
1    script  user    syslog              Off   Sun Feb 8 15:31:06 2009   transfer
-on-commit.tcl
 pattern {.*%SYS-5-CONFIG.*} nice 0 queue-priority normal maxrun 20.000

br0.lab3#sh event manager history events
No.  Time of Event             Event Type          Name
1    Sun Feb 8  15:16:50 2009  syslog              script: transfer-on-commit.tcl
2    Sun Feb 8  15:31:06 2009  syslog              script: transfer-on-commit.tcl
3    Sun Feb 8  15:31:08 2009  syslog              script: transfer-on-commit.tcl
4    Sun Feb 8  15:31:10 2009  syslog              script: transfer-on-commit.tcl
5    Sun Feb 8  15:55:29 2009  syslog              script: transfer-on-commit.tcl
6    Sun Feb 8  15:55:31 2009  syslog              script: transfer-on-commit.tcl
7    Sun Feb 8  15:55:34 2009  syslog              script: transfer-on-commit.tcl
8    Sun Feb 8  15:55:53 2009  syslog              script: transfer-on-commit.tcl
9    Sun Feb 8  15:55:54 2009  syslog              script: transfer-on-commit.tcl
10   Sun Feb 8  15:55:57 2009  syslog              script: transfer-on-commit.tcl
br0.lab3#

Below is my transfer-on-commit.tcl script. Don’t forget to change the settings. Please also read the comments before applying this on a live environment!

::cisco::eem::event_register_syslog pattern ".*%SYS-5-CONFIG.*"
 
## transfer-on-commit.tcl
#
#    This script will copy the running-config to a specified location
#    after the configuration has been changed.
#
#    For bugreports and other improvements contact Geert Hauwaerts
#
#        Email / MSN: geert@hauwaerts.be
#
#        Homepage:    http://www.hauwaerts.be
#        LinkedIN:    http://www.linkedin.com/in/GeertHauwaerts
#        Facebook:    http://www.facebook.com/people/Geert-Hauwaerts/656838360
##
 
## Importante note
#
#    This script must be named transfer-on-commit.tcl in order to prevent
#    a file copy loop!
#
#    After uploading use folowing command to enable this script. Please note
#    that your storage path might be different.
#
#        Router(config)#event manager directory user policy disk0:
#        Router(config)#event manager policy transfer-on-commit.tcl
##
 
##
# Change the settings to match your configuration.
##
 
set tftpserver "192.168.0.1"
set timestamp  [clock format [clock seconds] -format {%Y-%m-%d-%H%M%S}]
 
set routername [info hostname]
set location tftp://$tftpserver/$routername-$timestamp.conf
 
##
# Don't edit below this line!
##
 
namespace import ::cisco::eem::*
namespace import ::cisco::lib::*
 
array set arr_einfo [event_reqinfo]
set syslog_error_message $arr_einfo(msg)
 
if {[regexp {EEM:transfer-on-commit.tcl} $syslog_error_message]} {
	exit
}
 
if [catch {cli_open} result] {
	error $result $errorInfo
} else {
	array set cli1 $result
}
 
if [catch {cli_exec $cli1(fd) "enable"} result] {
	error $result $errorInfo
}
 
if [catch {cli_exec $cli1(fd) "config t"} result] {
    error $result $errorInfo
}
 
if [catch {cli_exec $cli1(fd) "file prompt quiet"} result] {
	error $result $errorInfo
}
 
if [catch {cli_exec $cli1(fd) "end"} result] {
	error $result $errorInfo
}
 
if [catch {cli_exec $cli1(fd) "copy running-config $location"} result] {
	error $result $errorInfo
}
 
if [catch {cli_exec $cli1(fd) "config t"} result] {
	error $result $errorInfo
}
 
if [catch {cli_exec $cli1(fd) "no file prompt quiet"} result] {
	error $result $errorInfo
}
 
if [catch {cli_exec $cli1(fd) "end"} result] {
	error $result $errorInfo
}

Comments (5)

TomFebruary 9th, 2009 at 9:54 am

Cute. :D

The advantage of having a scripting language on your router seems to come in pretty handy sometimes (though I bet most people don’t really use the Tcl interpreter on their Cisco’s). Thanks!

GeertFebruary 9th, 2009 at 9:47 pm

I have been using TCL scripts for ages. The new EEM is just heavenly, you can trigger on almost everything. Even add your own syslog messages or create menus for low-level NOC engineers :)

zizoudevinciJune 13th, 2009 at 1:38 pm

i tried this script with many ios version but it gives me syntax errors , is there any ios requirement to run it

GeertJuly 2nd, 2009 at 5:04 pm

No that I know. What is the syntax error you get? I have tested this on a Cisco 7200VXR.

sponnusaJanuary 5th, 2010 at 4:36 pm

I want to transfer a .zip or .tar file from one device to another device using TCL script.

could you pls help ?

Leave a comment

Your comment