update-dnsmasq 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/bash
  2. logger -t dhcpd "Refresh dnsmasq config request"
  3. force=$1
  4. /opt/Eye/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. [ ! -e "/etc/dnsmasq.d/" ] && mkdir -p "/etc/dnsmasq.d/"
  11. touch /etc/dnsmasq.d/mac-all
  12. 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)
  13. if [ -n "${CHANGES}" -o -n "${force}" ]; then
  14. logger -t dhcpd "Update dnsmasq config"
  15. cat /etc/dnsmasq.d/mac-all >/tmp/mac.old
  16. cat /tmp/mac-all >/etc/dnsmasq.d/mac-all
  17. /usr/sbin/dnsmasq --test >/dev/null 2>&1
  18. ret=$?
  19. if [ ${ret} -eq 0 ]; then
  20. #clear leases
  21. systemctl stop dnsmasq >/dev/null
  22. echo "${CHANGES}" | while read LEASE; do
  23. logger -t dhcpd "Clear ${LEASE}"
  24. if [ -n "${LEASE}" ]; then
  25. sed -i "/${LEASE}/d" /var/lib/misc/dnsmasq.leases
  26. fi
  27. done
  28. systemctl start dnsmasq >/dev/null
  29. else
  30. logger -t dhcpd "Config error! Rollback changes."
  31. cat /tmp/mac.old >/etc/dnsmasq.d/mac-all
  32. fi
  33. else
  34. logger -t dhcpd "Config not changed. Skip restart"
  35. fi
  36. logger -t dhcpd "done"
  37. exit