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.";
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