Skip to content

Instantly share code, notes, and snippets.

@psitem
Created February 27, 2025 16:26
Show Gist options
  • Save psitem/f84e9c5997ca86daa9c89976f710ebd3 to your computer and use it in GitHub Desktop.
Save psitem/f84e9c5997ca86daa9c89976f710ebd3 to your computer and use it in GitHub Desktop.
Create PTRs from dnsmasq's -dhcp-script command. Very quick-n-dirty, Works on My Active Directory™ tested.
$dnsServer = '10.10.10.10' # Replace with a DNS server accepting insecure updates.
$dnsDomain = 'home.mydomain.com' # Replace with your domain name.
$dhcpCmd = $args[0]
$dhcpMac = $args[1]
$dhcpIp = $args[2]
if ( $args.count -eq 4 ) {
$dhcpHostname = $args[3] -replace '[^a-zA-Z0-9-]', '-'
} else {
$dhcpHostname = $dhcpMac -replace ':','-'
}
$ptr = ($dhcpIp -replace '^(\d+)\.(\d+)\.(\d+).(\d+)$','$4.$3.$2.$1') + '.in-addr.arpa.'
$nsupdateCmd = @"
server $($dnsServer)
update delete $($ptr) PTR
$(if ($dhcpCmd -ne 'del') {"update add $($ptr) 300 PTR $($dhcpHostname).$($dnsDomain)."} )
send
quit
"@
$nsupdateCmd | nsupdate
@psitem
Copy link
Author

psitem commented Feb 27, 2025

dnsmasq sends output like:

cmd mac-address ip-address hostname

Where cmd can be add (lease added), old (lease update), or del (lease removed). Hostname is optional, if provided by the client. See the dnsmasq man page for more information on what gets passed as environmental variables.

Requires nsupdate, which is commonly installed by default on Linux distros.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment