2011-06-24 13:36:06 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
chronyc=/usr/bin/chronyc
|
|
|
|
dhclient_servers=/var/lib/dhclient/chrony.servers.*
|
2012-02-10 16:22:48 +00:00
|
|
|
dhclient_added_servers=/var/lib/dhclient/chrony.added_servers
|
2011-06-24 13:36:06 +00:00
|
|
|
|
|
|
|
chrony_command() {
|
2014-06-09 10:25:25 +00:00
|
|
|
$chronyc -a -n -m "$1"
|
2011-06-24 13:36:06 +00:00
|
|
|
}
|
|
|
|
|
2012-02-10 16:22:48 +00:00
|
|
|
update_dhclient_added_servers() {
|
|
|
|
new_servers=$(echo "$1" | sort -u)
|
|
|
|
old_servers=$(cat $dhclient_added_servers 2> /dev/null)
|
|
|
|
[ "$old_servers" = "$new_servers" ] && return 0
|
|
|
|
[ -n "$new_servers" ] && echo "$new_servers" > $dhclient_added_servers ||
|
|
|
|
rm -f $dhclient_added_servers
|
|
|
|
}
|
|
|
|
|
2011-06-24 13:36:06 +00:00
|
|
|
add_dhclient_servers() {
|
2012-02-10 16:22:48 +00:00
|
|
|
shopt -s nullglob
|
|
|
|
servers_files=($dhclient_servers)
|
|
|
|
shopt -u nullglob
|
|
|
|
(( ${#servers_files[*]} )) || return 0
|
|
|
|
|
|
|
|
added_servers=$(
|
|
|
|
cat $dhclient_added_servers 2> /dev/null
|
|
|
|
cat ${servers_files[*]} |
|
|
|
|
while read server serverargs; do
|
|
|
|
chrony_command "add server $server $serverargs" &> /dev/null &&
|
|
|
|
echo "$server"
|
|
|
|
done)
|
|
|
|
update_dhclient_added_servers "$added_servers"
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_dhclient_servers() {
|
|
|
|
[ -f $dhclient_added_servers ] || return 0
|
|
|
|
all_servers=$(
|
|
|
|
cat $dhclient_servers 2> /dev/null |
|
|
|
|
while read server serverargs; do
|
|
|
|
echo "$server"
|
|
|
|
done | sort -u)
|
|
|
|
echo "$all_servers" | comm -23 $dhclient_added_servers - |
|
|
|
|
while read server; do
|
|
|
|
chrony_command "delete $server" &> /dev/null
|
|
|
|
done
|
|
|
|
added_servers=$(echo "$all_servers" | comm -12 $dhclient_added_servers -)
|
|
|
|
update_dhclient_added_servers "$added_servers"
|
2011-06-24 13:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
is_running() {
|
2014-06-09 10:25:25 +00:00
|
|
|
chrony_command "tracking" &> /dev/null
|
2011-06-24 13:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "$1" in
|
|
|
|
add-dhclient-servers)
|
|
|
|
add_dhclient_servers
|
|
|
|
;;
|
2012-02-10 16:22:48 +00:00
|
|
|
remove-dhclient-servers)
|
|
|
|
remove_dhclient_servers
|
|
|
|
;;
|
|
|
|
is-running)
|
|
|
|
is_running
|
|
|
|
;;
|
2014-06-09 10:25:25 +00:00
|
|
|
command|forced-command)
|
2011-09-06 13:50:07 +00:00
|
|
|
chrony_command "$2"
|
|
|
|
;;
|
2011-06-24 13:36:06 +00:00
|
|
|
*)
|
2013-06-21 14:51:21 +00:00
|
|
|
echo $"Usage: $0 {add-dhclient-servers|remove-dhclient-servers|is-running|command|forced-command}"
|
2011-06-24 13:36:06 +00:00
|
|
|
exit 2
|
|
|
|
esac
|
|
|
|
exit $?
|
|
|
|
|