Dhcpd on commit
From Sidvind
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.
- # Update DNS (including static leases)
- ddns-update-style interim;
- update-static-leases on;
- ddns-domainname "example.net.";
- ddns-rev-domainname "in-addr.arpa.";
- # Add a hook which executes when a lease is added or updated
- on commit {
- # Get the client name from the first of the following:
- # 1. Client DHCP Option FQDN
- # 2. Client DHCP Option hostname
- # 3. Name of static lease (host-decl-name)
- # 4. A generated name "dyn-${ip}" where ip is the lease IP with dashes, e.g. "dyn-10-0-1-123"
- set clientddns = lcase(pick-first-value (option fqdn.hostname, option host-name, host-decl-name, concat ("dyn-", binary-to-ascii(10,8,"-", leased-address)), ""));
- # Get the client dynamic dns domain (different for each subnet)
- set clientdomain = config-option server.ddns-domainname;
- # Get client hostname only (no generated or static). Used by the hook to show which hostname the client requested.
- set clienthost = pick-first-value (option fqdn.hostname, option host-name, "");
- # Get lease IP and hwaddr
- set clientip = binary-to-ascii(10, 8, ".", leased-address);
- set clientmac = binary-to-ascii(16, 8, ":", substring(hardware, 1, 6));
- # Update the dynamic hostname (which wont be set unless the client requested one, so update with the generated if needed)
- ddns-hostname = clientddns;
- # Execute the hook
- execute("/path/to/my/hook", "commit", clientip, concat(clientddns, ".", clientdomain), clienthost, clientmac);
- }
- # Subnet 1
- subnet 10.0.1.0 netmask 255.255.255.0 {
- ddns-domainname "subnet1.example.net.";
- }
- # Subnet 2
- subnet 10.0.2.0 netmask 255.255.255.0 {
- ddns-domainname "subnet2.example.net.";
- }