Page 1 of 1

SetNtpServers - Set System Time via NTP, Alternative Script

Posted: Sat Oct 29, 2011 2:01 am
by TealFrog
I am providing this script as an alternative to the scripting example provided in the examples at the URL, http://wiki.mikrotik.com/wiki/Manual:Sc ... ce_for_NTP. The script works in a similar fashion using the ntp.org pool service to configure and set the NTP server IP addresses for the Mikrotik’s SNTP client configuration. The script I am providing is based on the example code at the noted link.

The script below is a simpler, smaller, single script that does not rely on global variables. This script does not use or send any email, so may not be a viable alternative for some users.

The script is rather easy to configure and use. Save the script under ROS and configure the script's name, set the "arrNtpSystems" array variable for the primary and secondary NTP pool based on information from http://www.pool.ntp.org. By default the script is configured for North America. Schedule the script to run on a periodic basis.
# SetNtpServers - Check and set NTP servers from NTP pool
# v1.2 Tested and Developed on ROS v5.7
#
# Change the following line as needed as progName should match script name 
:local progName "SetNtpServers";

# Array of NTP pools to use (check www.pool.ntp.org) one or a maximum of two, a primary & secondary
# Modify the following line and array variable based on your locale (default is north america).
:local arrNtpSystems ("0.north-america.pool.ntp.org", "1.north-america.pool.ntp.org");
# Alternatively the US related pool below can be used.  
#:local arrNtpSystems ("0.us.pool.ntp.org", "1.us.pool.ntp.org");
#
# No modification is necessary beyond this line.
:put "$progName: Running...";
:log info "$progName: Running...";
:set arrNtpSystems [ :toarray $arrNtpSystems ];
:if (( [ :len $arrNtpSystems ] < 1 ) or ( [ :len $arrNtpSystems ] > 2 )) do={ 
    :put "$progName: ERROR NTP Systems array (\$arrNtpSystems) must be either one or two DNS names.";
    :log info "$progName: ERROR NTP Systems array (\$arrNtpSystems) must be either one or two DNS names.";
} else={
    :local arrRosNtpSetting ("primary-ntp", "secondary-ntp");
    :local i 0;
    :foreach strNtpSystem in ($arrNtpSystems) do={
        :local ipAddrNtpSystem [ :resolve $strNtpSystem ];
        :local strRosNtpSetting [ :pick $arrRosNtpSetting $i ];
        :local strCurrentNtpIp [ /system ntp client get $strRosNtpSetting ];
        :put "$progName: NTP server DNS name $strNtpSystem resolves to $ipAddrNtpSystem.";
        :log info "$progName: NTP server DNS name $strNtpSystem resolves to $ipAddrNtpSystem.";
        :put "$progName: Current $strRosNtpSetting setting is $strCurrentNtpIp.";
        :log info "$progName: Current $strRosNtpSetting setting is $strCurrentNtpIp.";
        :if ( [ :toip $ipAddrNtpSystem ] != [ :toip $strCurrentNtpIp ] ) do={
            :put "$progName: Changing $strRosNtpSetting setting to $ipAddrNtpSystem.";
            :log info "$progName: Changing $strRosNtpSetting setting to $ipAddrNtpSystem.";
            :local strCommand [ :parse "/system ntp client set $strRosNtpSetting=\"$ipAddrNtpSystem\""  ];
            $strCommand;
        } else={
            :put "$progName: No changes were made for the $strRosNtpSetting NTP setting.";
            :log info "$progName: No changes were made for the $strRosNtpSetting NTP setting.";
        }
        :set i ($i + 1);
    }
}
:put "$progName: Done.";
:log info "$progName: Done.";
Create a scheduler entry similar to the following:
/system scheduler
add comment="Check and set NTP servers" disabled=no interval=12h name=\
    SetNtpServers on-event=SetNtpServers policy=read,write,test \
    start-date=oct/01/2011 start-time=00:00:00

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Sat Oct 29, 2011 10:35 pm
by SeaburyNorton
Thank you so much for this excellent script! I've been looking for something like this for a while now!


I can confirm it runs quite well on ROS 5.4.

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Tue Aug 07, 2012 12:28 pm
by gbh
great script, many thanks.

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Tue Aug 07, 2012 12:28 pm
by gbh
great script, many thanks.

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Thu Aug 08, 2013 12:08 pm
by milis
This script is great but I have problem with OS version 6.2. Somebody now solution, generally I am not good with scripting!
Log show that everything is ok but in ntp client no change!

Post is too old but maybe someone looking.

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Sat Mar 15, 2014 11:18 am
by icebox
Does this script work in 6.10?

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Mon Mar 24, 2014 10:15 pm
by xPucTu4
I have few routers that do not have internet access.
When I try to use some router for ntp server, on the clients I receive "server-not-synchronized" error.
With 6.11 (lastest). So I found a simple and dangerous solution if you have some spare linux box around.
You can change it to fit your network and add it to cron.
Also, you need sshpass. It is in most linux distributions now.
#!/bin/bash

#username and password
sshuser="admin"
passwrd="blablabla_this_is_not_my_real_password"

# targets
targs="255.2 255.4 255.6 255.12 255.35 255.37 72.3 72.20 72.21 72.96 68.1 68.245 68.246 0.4 0.26 0.50 0.61 0.62"

for rtr in $targs
do
    t=`date +"%H:%M:%S"`
    d=`date +"%b/%d/%Y"`
    executecmd='sshpass -p '$passwrd' ssh -oStrictHostKeyChecking=no -p 26 '$sshuser'@192.168.'$rtr' "/system clock set time='$t' date='$d' time-zone-name=America/Tortola"'
    eval $executecmd
done

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Sun Nov 09, 2014 3:21 am
by Alupis
I can confirm the NTP script works without modification on v6.21.1

I did notice it took a little bit before the time actually updated. Also, make sure you have specified your timezone in the System --> Clock menu.

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Sun Nov 09, 2014 9:58 am
by BartoszP
Isn't it easier to setup your local NTP server for your local network on this spare linux box ? Quick example: http://ubuntuforums.org/showthread.php?t=862620

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Sun Nov 09, 2014 6:48 pm
by gotsprings
/system ntp client set primary-ntp=[:resolve 0.pool.ntp.org]
/system ntp client set secondary-ntp=[:resolve 1.pool.ntp.org]

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Mon Nov 10, 2014 1:09 pm
by janisk
yes, put that in scheduler so you get updates.

In case you are not using NTP package, you can simply enter domain name into server-dns-names and for every update RouterOS will check for the IP. (Btw, that is comma separated list)

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Mon Nov 10, 2014 8:40 pm
by Alupis
Isn't it easier to setup your local NTP server for your local network on this spare linux box ? Quick example: http://ubuntuforums.org/showthread.php?t=862620
Sure, if you have one. Some folks just want their router to NTP itself... or don't have a spare box to setup NTP server on like in a SOHO environment.

Re: SetNtpServers - Set System Time via NTP, Alternative Scr

Posted: Tue Nov 11, 2014 10:54 am
by BartoszP
You are right but...
....So I found a simple and dangerous solution if you have some spare linux box around.....