02-dhcp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/sh
  2. # DHCP hotplug script - triggers on DHCP events
  3. # Install to /etc/hotplug.d/dhcp/
  4. #
  5. # Events emitted (must match client2server-unified.lua's handler):
  6. # dhcp_lease_new / dhcp_lease_expire
  7. #
  8. # Events are spooled to /var/run/client2server/buffer and the Lua agent
  9. # is woken via SIGHUP. If the agent is offline (internet down), events
  10. # accumulate on disk and are flushed on reconnect.
  11. SCRIPT_DIR="$(dirname "$0")"
  12. # Source the shared lib from its canonical install location.
  13. # Falls back to the development path (hotplug/_lib.sh) for tests.
  14. if [ -f /usr/share/client2server/hotplug-lib.sh ]; then
  15. # shellcheck disable=SC1091
  16. . /usr/share/client2server/hotplug-lib.sh
  17. elif [ -f "$SCRIPT_DIR/_lib.sh" ]; then
  18. # shellcheck disable=SC1091
  19. . "$SCRIPT_DIR/_lib.sh"
  20. else
  21. logger -t client2server-hotplug -p user.err "hotplug-lib.sh not found"
  22. exit 1
  23. fi
  24. # Handle DHCP events
  25. # add - new lease (or renew with new IP)
  26. # update - lease renewed (same IP, new expiry)
  27. # old - lease rebind/old
  28. # del - lease released
  29. # remove - alias for del in some builds
  30. case "$ACTION" in
  31. add|update|old)
  32. if [ -n "$MAC" ] && [ -n "$IP" ]; then
  33. log_msg "DHCP LEASE NEW: $MAC -> $IP ($HOSTNAME)"
  34. send_event "dhcp_lease_new" \
  35. "{\"mac\":\"$MAC\",\"ip\":\"$IP\",\"hostname\":\"$HOSTNAME\"}"
  36. fi
  37. ;;
  38. del|remove)
  39. if [ -n "$MAC" ] && [ -n "$IP" ]; then
  40. log_msg "DHCP LEASE EXPIRE: $MAC -> $IP"
  41. send_event "dhcp_lease_expire" \
  42. "{\"mac\":\"$MAC\",\"old_ip\":\"$IP\"}"
  43. fi
  44. ;;
  45. esac