Community discussions

MikroTik App
 
User avatar
TealFrog
just joined
Topic Author
Posts: 23
Joined: Sun Oct 02, 2011 11:56 am

SetNtpServers - Set System Time via NTP, Alternative Script

Sat Oct 29, 2011 2:01 am

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
 
User avatar
SeaburyNorton
Frequent Visitor
Frequent Visitor
Posts: 65
Joined: Tue Sep 28, 2010 9:39 pm

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

Sat Oct 29, 2011 10:35 pm

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.
 
gbh
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Mon Dec 08, 2008 1:05 am

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

Tue Aug 07, 2012 12:28 pm

great script, many thanks.
 
gbh
Frequent Visitor
Frequent Visitor
Posts: 52
Joined: Mon Dec 08, 2008 1:05 am

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

Tue Aug 07, 2012 12:28 pm

great script, many thanks.
 
milis
just joined
Posts: 4
Joined: Sun Jan 11, 2009 9:28 pm

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

Thu Aug 08, 2013 12:08 pm

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.
 
icebox
just joined
Posts: 3
Joined: Mon Oct 15, 2012 2:05 pm

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

Sat Mar 15, 2014 11:18 am

Does this script work in 6.10?
 
xPucTu4
just joined
Posts: 3
Joined: Sun Dec 06, 2009 3:10 pm

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

Mon Mar 24, 2014 10:15 pm

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
 
Alupis
just joined
Posts: 23
Joined: Wed Feb 29, 2012 6:30 pm

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

Sun Nov 09, 2014 3:21 am

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.
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 2865
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

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

Sun Nov 09, 2014 9:58 am

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
 
gotsprings
Forum Guru
Forum Guru
Posts: 2102
Joined: Mon May 14, 2012 9:30 pm

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

Sun Nov 09, 2014 6:48 pm

/system ntp client set primary-ntp=[:resolve 0.pool.ntp.org]
/system ntp client set secondary-ntp=[:resolve 1.pool.ntp.org]
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

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

Mon Nov 10, 2014 1:09 pm

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)
 
Alupis
just joined
Posts: 23
Joined: Wed Feb 29, 2012 6:30 pm

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

Mon Nov 10, 2014 8:40 pm

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.
 
User avatar
BartoszP
Forum Guru
Forum Guru
Posts: 2865
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland

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

Tue Nov 11, 2014 10:54 am

You are right but...
....So I found a simple and dangerous solution if you have some spare linux box around.....

Who is online

Users browsing this forum: karlisi, rogerioqueiroz and 17 guests