Created
July 18, 2023 20:02
-
-
Save johnlettman/191916aff4b694117bdbe00b6f610d3d to your computer and use it in GitHub Desktop.
Check upstream NTP connectivity
This file contains 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
#!/usr/bin/env bash | |
# shellcheck disable=SC2221,SC2222 | |
timeout=2; | |
urls=( | |
"ntp.ubuntu.com" | |
"pool.ntp.org" | |
); | |
function is_ipv4() { | |
local ip="$1"; | |
local IFS='.'; | |
local -a octets; | |
read -ra octets <<< "$ip"; | |
if (( ${#octets[@]} == 4 )); then | |
for octet in "${octets[@]}"; do | |
# return false if any octet is out of 8-bit range | |
if ! [[ $octet =~ ^[0-9]+$ ]] || (( octet > 255 || octet < 0 )); | |
then return 1; fi; | |
done; | |
else return 1; fi; # not four octets, return false | |
# return true if string passes all checks | |
return 0; | |
} | |
function get_ipv4() { | |
dig +noall +answer +short -tA "$1"; | |
} | |
function test_ntp() { | |
if ntpdate -q -t "${timeout}" "$1" >/dev/null 2>&1; then | |
echo -e "$1\tok"; | |
else | |
echo -e "$1\terr!"; | |
fi; | |
} | |
for url in "${urls[@]}"; do | |
if ! is_ipv4 "${url}"; then | |
echo -e "\n(${url})"; | |
for addr in $(get_ipv4 "${url}"); do | |
test_ntp "${addr}"; | |
done; | |
echo; | |
else test_ntp "${url}"; fi; | |
done; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment