Dhcpd on commit

From Sidvind
Jump to: navigation, search

Show how to configure dhcpd to execute a hook on commit (lease added or updated) containing:

  • Lease IP (10.0.x.y)
  • Client hwaddr (aa:bb:cc:dd:ee:ff)
  • Client requested hostname (foo)
  • Client DDNS FQDN (foo.example.net.)

It also handles when different subnets have different ddns-domainname and will generate client hostnames with dynamically generated ones if needed.

Code: dhcpd.conf (view, download)

  1. # Update DNS (including static leases)
  2. ddns-update-style interim;
  3. update-static-leases on;
  4. ddns-domainname "example.net.";
  5. ddns-rev-domainname "in-addr.arpa.";
  6.  
  7. # Add a hook which executes when a lease is added or updated
  8. on commit {
  9.   # Get the client name from the first of the following:
  10.   #   1. Client DHCP Option FQDN
  11.   #   2. Client DHCP Option hostname
  12.   #   3. Name of static lease (host-decl-name)
  13.   #   4. A generated name "dyn-${ip}" where ip is the lease IP with dashes, e.g. "dyn-10-0-1-123"
  14.   set clientddns = lcase(pick-first-value (option fqdn.hostname, option host-name, host-decl-name, concat ("dyn-", binary-to-ascii(10,8,"-", leased-address)), ""));
  15.  
  16.   # Get the client dynamic dns domain (different for each subnet)
  17.   set clientdomain = config-option server.ddns-domainname;
  18.  
  19.   # Get client hostname only (no generated or static). Used by the hook to show which hostname the client requested.
  20.   set clienthost = pick-first-value (option fqdn.hostname, option host-name, "");
  21.  
  22.   # Get lease IP and hwaddr
  23.   set clientip = binary-to-ascii(10, 8, ".", leased-address);
  24.   set clientmac = binary-to-ascii(16, 8, ":", substring(hardware, 1, 6));
  25.  
  26.   # Update the dynamic hostname (which wont be set unless the client requested one, so update with the generated if needed)
  27.   ddns-hostname = clientddns;
  28.  
  29.   # Execute the hook
  30.   execute("/path/to/my/hook", "commit", clientip, concat(clientddns, ".", clientdomain), clienthost, clientmac);
  31. }
  32.  
  33. # Subnet 1
  34. subnet 10.0.1.0 netmask 255.255.255.0 {
  35.   ddns-domainname "subnet1.example.net.";
  36. }
  37.  
  38. # Subnet 2
  39. subnet 10.0.2.0 netmask 255.255.255.0 {
  40.   ddns-domainname "subnet2.example.net.";
  41. }