SetNtpServers - Set System Time via NTP, Alternative Script
RouterOS Scripting and API

13 posts   •   Page 1 of 1
User avatar
TealFrog
just joined
 
Posts: 23
Joined: Sun Oct 02, 2011 11:56 am

SetNtpServers - Set System Time via NTP, Alternative Script

by TealFrog » 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:Scripting-examples#Allow_use_of_ntp.org_pool_service_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.

Code: Select all
# 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:
Code: Select all
/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
TealFrog

User avatar
SeaburyNorton
Frequent Visitor
Frequent Visitor
 
Posts: 63
Joined: Tue Sep 28, 2010 9:39 pm

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

by SeaburyNorton » 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 2:05 am

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

by gbh » Tue Aug 07, 2012 12:28 pm

great script, many thanks.

gbh
Frequent Visitor
Frequent Visitor
 
Posts: 52
Joined: Mon Dec 08, 2008 2:05 am

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

by gbh » Tue Aug 07, 2012 12:28 pm

great script, many thanks.

milis
just joined
 
Posts: 1
Joined: Sun Jan 11, 2009 10:28 pm

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

by milis » 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

by icebox » Sat Mar 15, 2014 12:18 pm

Does this script work in 6.10?

xPucTu4
just joined
 
Posts: 2
Joined: Sun Dec 06, 2009 4:10 pm

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

by xPucTu4 » Mon Mar 24, 2014 11: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.
Code: Select all
#!/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: 15
Joined: Wed Feb 29, 2012 7:30 pm

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

by Alupis » Sun Nov 09, 2014 4: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.

BartoszP
Frequent Visitor
Frequent Visitor
 
Posts: 83
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland, 43-300

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

by BartoszP » Sun Nov 09, 2014 10: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
It is safer to feed my Karma than bears in the ZOO.

gotsprings
Member Candidate
Member Candidate
 
Posts: 202
Joined: Mon May 14, 2012 9:30 pm

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

by gotsprings » Sun Nov 09, 2014 7:48 pm

Code: Select all
/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: 5925
Joined: Tue Feb 14, 2006 10:46 am
Location: Riga, Latvia

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

by janisk » Mon Nov 10, 2014 2: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: 15
Joined: Wed Feb 29, 2012 7:30 pm

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

by Alupis » Mon Nov 10, 2014 9:40 pm

BartoszP wrote: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.

BartoszP
Frequent Visitor
Frequent Visitor
 
Posts: 83
Joined: Mon Jun 16, 2014 1:13 pm
Location: Poland, 43-300

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

by BartoszP » Tue Nov 11, 2014 11:54 am

You are right but...
xPucTu4 wrote:....So I found a simple and dangerous solution if you have some spare linux box around.....
It is safer to feed my Karma than bears in the ZOO.

13 posts   •   Page 1 of 1

Who is online

Users browsing this forum: No registered users and 9 guests

It is currently Tue Dec 23, 2014 5:14 am