Skip to content

Instantly share code, notes, and snippets.

@ste-fan
Last active February 26, 2016 22:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ste-fan/7b9b5bcf6656f59c280f to your computer and use it in GitHub Desktop.
Save ste-fan/7b9b5bcf6656f59c280f to your computer and use it in GitHub Desktop.
ntpdate-debian configuration file that screens NetworkManager's latest DHCP leases for NTP servers
# The settings in this file are used by the program ntpdate-debian, but not
# by the upstream program ntpdate.
# Set to "yes" to take the server list from /etc/ntp.conf, from package ntp,
# so you only have to keep it in one place.
NTPDATE_USE_NTP_CONF=yes
# List of NTP servers to use (Separate multiple servers with spaces.)
# Not used if NTPDATE_USE_NTP_CONF is yes.
NTPSERVERS="ntp.ubuntu.com"
# Additional options to pass to ntpdate
NTPOPTIONS=""
# extract NTP servers from latest DHCP lease (if option was given by server)
get_debug_opts() {
local opt
debug_opt=false
debug_syslog=false
for opt in "$@" $( xargs -0 < /proc/$$/cmdline ); do
case "$opt" in
-d) debug_opt=true ;;
-s) debug_syslog=true ;;
esac
done
return 0
}
dhcp_ntp_debug_msg() {
if $debug_opt; then
echo "$(LC_ALL=C date +'%_d %b %H:%M:%S')" "ntpdate[$$]:" \
'(DCHP)' "$@"
if $debug_syslog; then
logger --tag "ntpdate[$$]" '(DCHP)' "$@"
fi
fi
return 0
}
get_dhcp_ntp_servers() {
local debug_opt debug_syslog leasefiledir recentleasefile ntpservers
leasefiledir=/var/lib/NetworkManager
# check whether we are in debug mode and logging to syslog
get_debug_opts $NTPOPTIONS
# get most recent lease file that is not older than 1 minute
recentleasefile=$( find $leasefiledir ! -type d -iname '*.lease' \
! -mmin +1 -printf '%T@ %p\n' 2>/dev/null \
| sort -k 1nr | sed 's/^\S\+ //; 1q' )
if [ -n "$recentleasefile" ]; then
dhcp_ntp_debug_msg 'Found a recent lease file:' \
$( basename $recentleasefile )
# extract NTP servers, if any (check only latest lease in file)
ntpservers=$( sed '1,/lease\s\+{/d
/option ntp-servers/!d
s/^.*ntp-servers\s\+\(.\+\);$/\1/; s/,/ /g' \
$recentleasefile )
if [ -n "$ntpservers" ]; then
dhcp_ntp_debug_msg 'Found NTP server(s) in latest lease:' \
$ntpservers
NTPSERVERS="$ntpservers"
else
dhcp_ntp_debug_msg 'Latest lease does not contain any NTP servers.'
fi
else
dhcp_ntp_debug_msg 'There is no recent lease file.'
fi
return 0
}
get_dhcp_ntp_servers
unset get_debug_opts dhcp_ntp_debug_msg get_dhcp_ntp_servers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment