cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
779
Views
2
Helpful
3
Replies

EEM: Parsing CLI output from "show clock" yields unexpected results

6502
Level 1
Level 1

I wrote an EEM applet that runs the "show clock" CLI command, then tried to parse the output using the "action string range" statement.  I wanted to get the 2 digit hour and 2 digit minute into two seperate varaibles.  So I used something like:

action xxx string range $_cli_result 0 1
action xxx set hour $_string_result
action xxx string range $_cli_result 3 4
action xxx set minute $_string result

This didn't work however, as there appear to be some non-printing/control characters at the front of the string that caued the "string range" indexing not to work properly.  In fact I couldn't get it to return the tens digit of the hour at all, I just kept getting what appeared on the screen as a blank character.

Does anyone know what's going on with the "show clock" command output?  In the end, I ended up using the bult-in "$_event_pub_time" to get a useable timestamp string to parse.  The problem with THAT method is that "$_event_pub_time" doesn't pay attention to the timezone in the configuration and always returns UTC time.  So then I have to do math in EEM to get the correct time...

1 Accepted Solution

Accepted Solutions

Dan Frey
Cisco Employee
Cisco Employee

EEM aligns with TCL and the event_pub_time grabs this info from gettime() in linux.   linux being the operating system and IOS-XE is a process.   IOS-XE cli massages gettime() to a user configurable timezone if required but his does not change the underlying linux OS function.   If you want the time to get reported by the specific timezone configured I believe you are on the right track by issuing "show clock" and the info can be parsed out by regexp rather than a string command.

event manager applet test
 event none
 action 010 cli command "enable"
 action 030 cli command "show clock"
 action 050 regexp "([0-9]+):([0-9]+):[0-9]+\.[0-9]+" "$_cli_result" match hour minute
 action 060 puts "$hour:$minute"
INET#event manager run test
11:56

View solution in original post

3 Replies 3

Dan Frey
Cisco Employee
Cisco Employee

EEM aligns with TCL and the event_pub_time grabs this info from gettime() in linux.   linux being the operating system and IOS-XE is a process.   IOS-XE cli massages gettime() to a user configurable timezone if required but his does not change the underlying linux OS function.   If you want the time to get reported by the specific timezone configured I believe you are on the right track by issuing "show clock" and the info can be parsed out by regexp rather than a string command.

event manager applet test
 event none
 action 010 cli command "enable"
 action 030 cli command "show clock"
 action 050 regexp "([0-9]+):([0-9]+):[0-9]+\.[0-9]+" "$_cli_result" match hour minute
 action 060 puts "$hour:$minute"
INET#event manager run test
11:56

Thamks @Dan Frey! Can you please parse out & explain the regexp terms as I think that will be helpful for the community going forward (though it would be better if the “noise” wasn’t in the output in the first place. ;))

balaji.bandi
Hall of Fame
Hall of Fame

have you look at the sample script :

https://github.com/leonmflai/Cisco-EEM-Auto-Export

BB

***** Rate All Helpful Responses *****

How to Ask The Cisco Community for Help

Review Cisco Networking for a $25 gift card