Skip to content

Instantly share code, notes, and snippets.

@selvakn
Last active April 21, 2019 05:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save selvakn/7a402804bf65e9bf29a39f16ec6c1544 to your computer and use it in GitHub Desktop.
Save selvakn/7a402804bf65e9bf29a39f16ec6c1544 to your computer and use it in GitHub Desktop.
Forward DHCP events from isc-dhcp-server server
# Add the following snippet to your dhcpd.conf
commit {
set ClientName = pick-first-value(option fqdn.hostname, option host-name);
set ClientIp = binary-to-ascii(10, 8, ".", leased-address);
set ClientMac = binary-to-ascii(16, 8, ":", substring(hardware, 1, 6));
execute("forward_dhcp_events.py", "commit", ClientName, ClientIp, ClientMac);
}
on release {
set ClientName = pick-first-value(option fqdn.hostname, option host-name);
set ClientIp = binary-to-ascii(10, 8, ".", leased-address);
set ClientMac = binary-to-ascii(16, 8, ":", substring(hardware, 1, 6));
execute("forward_dhcp_events.py", "release", ClientName, ClientIp, ClientMac);
}
#!/usr/bin/env python
import socket
import json
import os
import sys
UDP_IP = "127.0.0.1"
UDP_PORT = 1234
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
message = {
"type": "DHCPServer",
"action": sys.argv[1],
"name": sys.argv[2],
"ip": sys.argv[3],
"mac": sys.argv[4]
}
sock.sendto(json.dumps(message) + "\n", (UDP_IP, UDP_PORT))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment