update-dnsmasq 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/bash
  2. logger -t dhcpd "Refresh dnsmasq config request"
  3. force=$1
  4. /usr/local/scripts/print-dnsmasq.pl >/tmp/mac-all
  5. ret=$?
  6. if [ ${ret} -ne 0 ]; then
  7. logger -t dhcpd "Config generate error! Bye."
  8. exit
  9. fi
  10. touch /etc/dnsmasq.d/mac-all
  11. CHANGES=$(diff -ubBi /tmp/mac-all /etc/dnsmasq.d/mac-all | egrep "^[+-]dhcp" | awk '{ print $1 }' | sed -E 's/(\+|\-)//;s/dhcp-host=//;s/\,//' | sort -u)
  12. if [ -n "${CHANGES}" -o -n "${force}" ]; then
  13. logger -t dhcpd "Update dnsmasq config"
  14. cat /etc/dnsmasq.d/mac-all >/tmp/mac.old
  15. cat /tmp/mac-all >/etc/dnsmasq.d/mac-all
  16. /usr/sbin/dnsmasq --test >/dev/null 2>&1
  17. ret=$?
  18. if [ ${ret} -eq 0 ]; then
  19. #clear leases
  20. systemctl stop dnsmasq >/dev/null
  21. echo "${CHANGES}" | while read LEASE; do
  22. echo "Clear ${LEASE}"
  23. sed -i "/${LEASE}/d" /var/lib/misc/dnsmasq.leases
  24. done
  25. systemctl start dnsmasq >/dev/null
  26. else
  27. logger -t dhcpd "Config error! Rollback changes."
  28. cat /tmp/mac.old >/etc/dnsmasq.d/mac-all
  29. fi
  30. else
  31. logger -t dhcpd "Config not changed. Skip restart"
  32. fi
  33. logger -t dhcpd "done"
  34. exit