Last active
November 24, 2022 18:27
-
-
Save colorwebdesigner/9ee9e7137a30e7caca08e958e5fd4d48 to your computer and use it in GitHub Desktop.
Mikrotik script (.sh only for syntax highlighting)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================== | |
# setNtpServers | |
# ============================== | |
# Mikrotik RB951G-2HnD | |
# RouterOS v6.46.4 (stable) | |
# --- | |
# Corrects IP addresses of NTP servers in | |
# System -> NTP Client (if they have changed), | |
# resolve IPs by NTP domains and send message | |
# to Slack. | |
# ============================== | |
# Script start | |
{ | |
# OPTIONS | |
# --- | |
# [1] Specify NTP domains | |
# https://www.ntppool.org | |
# Must be either one or two DNS names. | |
:local arrNtpSystems ("0.XX.pool.ntp.org", "1.XX.pool.ntp.org"); | |
# [2] Your Slack Incoming Webhook URL | |
# https://api.slack.com/messaging/webhooks | |
# Keep it secret, keep it safe. Your webhook URL contains a secret. | |
# Don't share it online, including via public version control repositories. | |
# Slack actively searches out and revokes leaked secrets. | |
:local slackHookUrl "https://hooks.slack.com/services/X0000000000/Y0000000/Z0000000000000000000000"; | |
# [3] Specify a title for the message | |
# Formatting: https://api.slack.com/reference/surfaces/formatting | |
# Builder: https://api.slack.com/tools/block-kit-builder | |
:local title "Mikrotik -> setNtpServers:"; | |
# --- | |
# DO THE JOB | |
:set arrNtpSystems [ :toarray $arrNtpSystems ]; | |
:if (( [ :len $arrNtpSystems ] < 1 ) or ( [ :len $arrNtpSystems ] > 2 )) do={ | |
:local msg "$title Error at \$arrNtpSystems variable. Must be either one or two DNS names."; | |
:local dataForHook "{\"text\":\"$msg\"}"; | |
:log error $msg; | |
/tool fetch http-data=$dataForHook url=$slackHookUrl; | |
} 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 ]; | |
:log info "$title NTP server DNS name $strNtpSystem resolves to $ipAddrNtpSystem."; | |
:log info "$title Current $strRosNtpSetting setting is $strCurrentNtpIp."; | |
:if ( [ :toip $ipAddrNtpSystem ] != [ :toip $strCurrentNtpIp ] ) do={ | |
:local msg "$title Changing $strRosNtpSetting setting to $ipAddrNtpSystem."; | |
:local dataForHook "{\"text\":\"$msg\"}"; | |
:log info "$msg"; | |
:local strCommand [ :parse "/system ntp client set $strRosNtpSetting=\"$ipAddrNtpSystem\"" ]; | |
$strCommand; | |
/tool fetch http-data=$dataForHook url=$slackHookUrl; | |
} else={ | |
:log info "$title No changes were made for the $strRosNtpSetting NTP setting."; | |
} | |
:set i ($i + 1); | |
} | |
} | |
:log info "$title Done."; | |
} | |
# Script end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
setNtpServers
Corrects IP addresses of NTP servers in
System -> NTP Client
(if they have changed), resolve IPs by NTP domains and send message to Slack.The main idea:
Mikrotik RB951G-2HnD
RouterOS v6.46.4 (stable)