update-dnsmasq 1.4 KB

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