Syncing IOS Clock from Cellular Provider

I recently had a request to enable time synchronization from a Cellular provider to a 3G model of the Cisco 819. Looking through several documentation sources, I found an example of EEM policy utilizing GPS data in this manner.

LTE GPS Antenna Guide Cisco Integrated Services Router (ISR G2) and Connected Grid Router

After looking at the TCL script outlined in the above document, I thought it would be an easy modification to achieve this result with the cellular network data. After fighting with the script and EEM policy for a couple of hours, I stepped back and looked at the options for creating an EEM Applet. My goal was to achieve similar results but utilizing the time provided by the cellular carrier. This article outlines my process and the final configuration.

The source of the data that I wanted to use was derived from the show cell 0 network command.

CiscoRTR#show cell 0 network
Current Service = 1xEV-DO (Rev A) and 1xRTT
Current Roaming Status(1xRTT) = HOME, (HDR) = HOME
Current Idle Digital Mode = HDR
Current System Identifier (SID) = DDDD
Current Network Identifier (NID) = DDD
Current Call Setup Mode = Mobile IP only
Serving Base Station Longitude = -DD deg -D min -DD sec
Serving Base Station Latitude = DD deg D min DD sec
Current System Time = Tue Jan 19 18:32:21 2016

The very last line is the focus of interest and the challenge was how to reformat that into a command that will set the clock on the router. In short, I needed an applet to do the following.

CiscoRTR>enable
CiscoRTR#show cell 0 network | include Time
Current System Time = Tue Jan 19 18:32:21 2016
//need to munge the above line to acceptable parameters for "clock set xx" 
//Tue Jan 19 18:32:21 2016 -> 18:32:21 Jan 19 2016

//Then execute "clock set" with that as a parameter on the router
CiscoRTR#clock set 18:32:21 Jan 19 2016

Obviously it also needed to be configured to execute on a scheduled basis. So working through the process, I found that the following achieved the goal. Note that I used event none initially for manual testing.

event manager applet CELL_TIME
 event none
 action 1.0 cli command "enable"
 action 2.0 cli command "show cell 0 network | include Time"
 
 !take the result of above, confirm the match and separate it to time like datetime like (Thu Jan 21 16:37:12 2016)
 action 3.0 regexp "Current System Time = (.*)" "$_cli_result" _match _sub1
 action 3.1 set datetime "$_sub1"

 !pull the day "Thu" off of datetime
 action 4.0 regexp "([A-Z][a-z][a-z]) (.*)" "$datetime" _match _sub1 _sub2
 action 4.1 set datetimend "$_sub2"

 !parse out Jan 21 16:37:12 2016 to $_sub1=Jan 21, $_sub2=16:37:12, $_sub3=2016
 action 5.0 regexp "(.*) ([0-9]+:[0-9]+:[0-9]+) ([0-9][0-9][0-9][0-9])" "$datetimend" _match _sub1 _sub2 _sub3

 !execute the command "clock set 16:37:12 Jan 21 2016"
 action 6.0 cli command "enable"
 action 6.1 cli command "clock set $_sub2 $_sub1 $_sub3"

All of the lines beginning with a “!” are comments to describe the function. Care must be taken if using a text editor to ensure the quotes aren’t changed in any way. If there is a need get additional output during the debug phase, the final action can be eliminated and additional actions inserted as required. I used the puts action quite a bit to confirm the current value in the variables. Since event none precludes any form of automatic execution, this is done manually from privilege exec mode.

Manually Executing CELL_TIME

CiscoRTR#event manager run CELL_TIME

After manual execution, we have an opportunity to check for the desired output (clock update, debug output logged, or additional information that would be gleaned from commands inserted for debugging).

Once this worked as expected, I changed the event from  event none, to a time based execution. In my case, I used cron to execute the command every 15 minutes.

event manager applet CELL_TIME
 event timer cron cron-entry "*/15 * * * *" 
 action 1.0 cli command "enable"
 action 2.0 cli command "show cell 0 network | include Time"
 action 3.0 regexp "Current System Time = (.*)" "$_cli_result" _match _sub1
 action 3.1 set datetime "$_sub1"
 action 4.0 regexp "([A-Z][a-z][a-z]) (.*)" "$datetime" _match _sub1 _sub2
 action 4.1 set datetimend "$_sub2"
 action 5.0 regexp "(.*) ([0-9]+:[0-9]+:[0-9]+) ([0-9][0-9][0-9][0-9])" "$datetimend" _match _sub1 _sub2 _sub3
 action 6.0 cli command "enable"
 action 6.1 cli command "clock set $_sub2 $_sub1 $_sub3"

Caveats

  • This configuration is hardcoded to Cellular 0 interface
  • It is only as accurate as the time provided by the cellular provider
  • NTP should be disabled
  • The timezone on the router needs to be set to match what time is provided by Cellular provider

Conclusion

This is sort of a hack, but it seems to work pretty well. If someone knows of a better way to achieve this result, I’d definitely like to hear about it.

Disclaimer: This article includes the independent thoughts, opinions, commentary or technical detail of Paul Stewart. This may or may does not reflect the position of past, present or future employers.

No related content found.

About Paul Stewart, CCIE 26009 (Security)

Paul is a Network and Security Engineer, Trainer and Blogger who enjoys understanding how things really work. With over 15 years of experience in the technology industry, Paul has helped many organizations build, maintain and secure their networks and systems.
This entry was posted in How-To. Bookmark the permalink.