cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
797
Views
6
Helpful
17
Replies

EEM Script to fix Cisco router NTP server drift

s.maxina1
Level 1
Level 1

Hi All.

for some reason i need to configure cisco router as a ntp server( ntp master command).  But per 24 hours, it will be drifted about 25 seconds which i need to fix it. i want to execute an eem script so that at clock 00:00:00, add 25 seconds to router clock. 
Any idea?

note: at this time, i dont wnat to use external NTP Server.

thanks.

17 Replies 17

Enes Simnica
Level 5
Level 5

gDay to u @s.maxina1 Using the router as an NTP master is possible, but it’s not recommended for accurate timekeeping since Cisco devices don’t have high-precision hardware clocks. An EEM script could adjust the time daily, but it would still be unreliable and might cause issues for clients relying on NTP. The best long-term fix is to sync with an external NTP source (even an internal stratum server if you can’t reach the internet). If that’s not an option, u can script the clock adjustment, but expect some ongoing drift..... 

also I draft an example EEM script, if u want me to, let me know!

hope it helps and PEACE!

 

-Enes

more Cisco?!
more Gym?!



If this post solved your problem, kindly mark it as Accepted Solution. Much appreciated!

Hi. I know that it is not right solution. Would you send me EEM Script? I have to say that i ask chatgpt and it gives me some codes but it doesn’t work. The tcl script works good manually(tclsh command) but when i configure it in EEM Script in config mode, some command rejected. These command as follows:

event manager applet Fix-Time
event timer cron name FIXTIME cron-entry "0 0 * * *"
action 1.0 cli command "enable"
action 1.1 cli command "tclsh"
action 1.2 cli command {
set now [clock seconds]
set newtime [expr {$now + 25}]
set date [clock format $newtime -format "%H:%M:%S %d %b %Y"]
puts "New time: $date"
exec clock set $date
}
action 1.3 cli command "exit"
thanks.

 

yesssirr., ANd i can see right away EXACTLY why ur eem script fails. What’s happening is that EEM applets don’t allow entering the tclsh interpreter inside the script. When u run action 1.1 cli command "tclsh", the applet gets stuck because EEM doesn’t handle interactive interpreters, that’s why those lines are rejected. And the solution is this 1: U cannot embed a full Tcl block directly inside an EEM applet with cli command "tclsh". and 2: instead, u have twho valid approaches 

Option 1 make sure to use an eem policy written directly in tcl (meaning instead of an applet, u creat an eem tcl policy file and register it, and this is my script that i have for those situations:

::cisco::eem::event_register_timer cron name FIXTIME cron_entry "0 0 * * *"

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

proc fixTime {} {
    set now [clock seconds]
    set newtime [expr {$now + 25}]
    set date [clock format $newtime -format "%H:%M:%S %d %b %Y"]
    puts "New time: $date"

    cli_open
    cli_exec "enable"
    cli_exec "clock set $date"
    cli_close
}

fixTime

and THEN SAVE AS fix-time.tcl in flash and register it with eem

that way the script OR THEE script LOOL runs natively under eem withou trying to call tclsh inside an applet.

OPtion 2, is to stay with applet but avoid tclsh wiht this scripts;

event manager applet Fix-Time
 event timer cron name FIXTIME cron-entry "0 0 * * *"
 action 1.0 cli command "enable"
 action 1.1 cli command "clock set 00:00:25 25 Aug 2025"

AND ADJUST STUFF AS PER UR NEED............... HEHE

sooO long story short: Ur script fails because EEM applets cannot spawn tclsh. If u need Tcl logic, write it as a standalone EEM Tcl policy (.tcl file in flash) and register it with EEM.......

and i can tell u I had fun with this, and I hope u have time to read it, CAUSE THATS THE SCRIPT THAT I USE FOR SITUATIONS LIKE THIS.

HOPe it helps, enjoy ur weekend, and PEACE!!

 

-Enes

more Cisco?!
more Gym?!



If this post solved your problem, kindly mark it as Accepted Solution. Much appreciated!

For the seconds solution, since we need do this in each day, so the date is problem and must change every day. 
for first solution, error “ no value given for parameter “cmd” to “cli_exec”

my event manager version is 3.20

i search the web and find these commands work:

array set cli_arr [cli_open]
cli_exec $cli_arr(fd) “enable”

cli_exec $cli_arr(fd) “clock set $date”

cli_close 

the only problem is that the command cli_close not working 

Stefan Mihajlov
Level 1
Level 1

@s.maxina1 

you can hack it, but it’s a bad idea.

  • ntp master without an upstream reference will never discipline the router’s clock. A 25s/day drift is the oscillator; NTP can’t fix that locally.

  • Stepping time daily breaks stuff (logs, syslog correlation, certs, OSPF/HSRP timers, TACACS). IOS can only step with clock set, not gently slew.

M02@rt37
VIP
VIP

Hello @s.maxina1 

For any production deployment, this method is not reliable — it will not correct long-term drift accurately, and it can cause issues for devices depending on precise time. In production, it’s recommended to use a proper NTP source, such as a GPS stratum‑1 server or a dedicated internal ntp server...

 

Best regards
.ı|ı.ı|ı. If This Helps, Please Rate .ı|ı.ı|ı.

event manager applet MHM1
event timer cron cron-entry "0 0 * * *" <<- Trigger at 00:00:00 every day
action 1.0 cli command "enable"
action 2.0 cli command "clock set 00:00:25"

Just a quick note. I would add one more line to the script:

action 3.0 syslog msg "system clock set to 00:00:25 to compensate for NTP drift."

 This allows a simple check to verify that the script is running correctly.

Of course, as already mentioned this should be just a workaround and the use of a GPS or radio controlled clock is highly recommended.

Hi. I think you forgot the date parameters( day month and year). Incomplete command “clock set 00:00:25”.

Thanks' you are correct

And please add @Jens Albrecht  line it help you in troubleshooting and know if EEM work or not.

MHM

event manager applet ADD_25S_AT_MIDNIGHT
event timer cron cron-entry "0 0 * * *" ! Trigger at 00:00:00 every day
action 1.0 cli command "enable"
action 2.0 cli command "show clock"
action 3.0 regexp ".* ([A-Za-z]+) ([0-9]+) [0-9]+$" "$_cli_result" match month day year
action 4.0 cli command "conf t"
action 5.0 cli command "clock set 00:00:25 $day $month $year"
action 6.0 cli command "end"

When you mentioned day/months I start thinking previous eem will add same day each time it run

So solution we need to run show clock and take day and months then add it again to set clock +25 sec command

So try above

MHM

Please try it in lab before run it in production network, in case there is something wrong 

Please add Eem with different name and remove old eem 

MHM

Not working. We dont need tclsh command?

also in action 4, you use “conf t” but for set time we do it in enable mode and not configure mode

How you test it?

When you test to return clock to 11:57 then check?

Change the command 

Config t >>enable 

Run debug event manager all <<- share it here 

MHM