Recently ine ran a challenge for EEM INE BLOG

I have yet to here anything from ine so I’m posting my code below.

############################################################################################
# CISCO TCL EEM syslog config autowrite
# Version 0.3
# 1/13/2012
# Justin Guagliata
# Copyright 2012 @ ensgrp.com
#
# Copy the script to Flash and enter the following two commands in config mode
# event manager directory user policy “flash:/”
# event manager policy SYSLOG_CONFIG.tcl
#
############################################################################################

############################################################################################
# Monitor SYSLOG output for the “SYS-5-CONFIG.*” pattern
# run with a low priority and nice
# Set a max execution time of 60 seconds

::cisco::eem::event_register_syslog occurs 1 pattern “SYS-5-CONFIG.*” maxrun 60 queue_priority low nice 1

############################################################################################
# Import the EEM Libraries to use in this TCL scipt

namespace import ::cisco::eem::*
namespace import ::cisco::lib::*

############################################################################################
# Set FTP server

set FTP_SERVER “10.0.0.1″

############################################################################################
# System variables

set ROUTER_NAME [info hostname]
set CUR_TIME_DATE [clock format [clock seconds] -format “%Y-%m-%d-%H%M%S”]

############################################################################################
# EEM – Opens a connection to the CLI

if [catch {cli_open} result] {
error $result $errorInfo
} else {
array set cli1 $result
}

############################################################################################
# EEM – Run CLI Commands

if [catch {cli_exec $cli1(fd) "enable"} result] {
error $result $errorInfo
}

############################################################################################
# Get Last user to change config.

if [catch {cli_exec $cli1(fd) "sh configuration id detail | inc Changed by user"} result] {
error $result $errorInfo
} else {
set cmd_output $result

set prompt [format "(.*\n)(%s)(\\(config\[^\n\]*\\))?(#|>)” $ROUTER_NAME]
if [regexp "[set prompt]” $result dummy cmd_output] {
# do nothing, match will be in $cmd_output
} else {
# did not match router prompt so use original output
set cmd_output $result
}

# remove white space from output
set cmd_output [regexp -inline -all -- {\S+} $cmd_output]
# Remove the leading description by stripping the first 17 characters
# this leaves us just the username
set CURRENT_USER [string replace $cmd_output 0 17 ""]
}

############################################################################################
# If CURRENT_USER returns empty we know that the config was last written by this script
# For tacacs enviroments this will be changed to the eem user

if {$CURRENT_USER != “”} {
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 tftp://$FTP_SERVER/$ROUTER_NAME.$CUR_TIME_DATE.$CURRENT_USER.working.cfg"} result] {
error $result $errorInfo
}
}
# Close open cli before exit.
if [catch {cli_close $cli1(fd) $cli1(tty_id)} result] {
error $result $errorInfo
} else {
exit 1
}

############################################################################################
# CISCO TCL EEM autowrite
# Version 0.4
# 1/13/2012
# Justin Guagliata
# Copyright 2012 @ ensgrp.com
#
# Copy the script to Flash and enter the following two commands in config mode
# event manager directory user policy “flash:/”
# event manager policy autowrite.tcl
#
############################################################################################

############################################################################################
# Register EEM for the following patterns “wr.*|wr.* mem.*|copy ru.* st.*”
# This policy is run
# Set a max execution time of 60 seconds

::cisco::eem::event_register_cli pattern “wr.*|wr.* mem.*|copy ru.* st.*” sync yes maxrun 60

############################################################################################
# Import the EEM Libraries to use in this TCL scipt

namespace import ::cisco::eem::*
namespace import ::cisco::lib::*

############################################################################################
# Set FTP server

set FTP_SERVER “10.0.0.1″

############################################################################################
# System variables

set ROUTER_NAME [info hostname]
set CUR_TIME_DATE [clock format [clock seconds] -format “%Y-%m-%d-%H%M%S”]

############################################################################################
# EEM – Opens a connection to the CLI

if [catch {cli_open} result] {
error $result $errorInfo
} else {
array set cli1 $result
}

############################################################################################
# EEM – Run CLI Commands

if [catch {cli_exec $cli1(fd) "enable"} result] {
error $result $errorInfo
}

############################################################################################
# Get Last user to change config.

if [catch {cli_exec $cli1(fd) "sh configuration id detail | inc Changed by user"} result] {
error $result $errorInfo
} else {
set cmd_output $result

set prompt [format "(.*\n)(%s)(\\(config\[^\n\]*\\))?(#|>)” $ROUTER_NAME]
if [regexp "[set prompt]” $result dummy cmd_output] {
# do nothing, match will be in $cmd_output
} else {
# did not match router prompt so use original output
set cmd_output $result
}

# remove white space from output
set cmd_output [regexp -inline -all -- {\S+} $cmd_output]
# Remove the leading description by stripping the first 17 characters
# this leaves us just the username
set CURRENT_USER [string replace $cmd_output 0 17 ""]
}

############################################################################################
# If CURRENT_USER returns empty we know that the config was last written by this script
# For tacacs enviroments this will be changed to the eem user

if {$CURRENT_USER != “”} {
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) "wr mem"} result] {
error $result $errorInfo
}

if [catch {cli_exec $cli1(fd) "copy startup-config tftp://$FTP_SERVER/$ROUTER_NAME.$CUR_TIME_DATE.$CURRENT_USER.startup.cfg"} result] {
error $result $errorInfo
}
}
############################################################################################
# Close open cli before exit. Exit code 0 means don’t perform the command which was caught by the script (wr mem)
# an exit code of 1 would write the script
if [catch {cli_close $cli1(fd) $cli1(tty_id)} result] {
error $result $errorInfo
} else {
exit 0
}

Comment now!
















Trackbacks