Skip to content

Instantly share code, notes, and snippets.

@i8degrees
Created May 15, 2025 18:11
Show Gist options
  • Save i8degrees/38b27a0a33e0ea9fc05d7acd8a71e2e4 to your computer and use it in GitHub Desktop.
Save i8degrees/38b27a0a33e0ea9fc05d7acd8a71e2e4 to your computer and use it in GitHub Desktop.
#!/bin/sh
# to use:
# write me to /usr/local/bin/dnsmasq-nsupdate.sh
# chmod +x /usr/local/bin/dnsmasq-nsupdate.sh
# append to /etc/dnsmasq.conf:
# dhcp-script=/usr/local/bin/dnsmasq-nsupdate.sh
# in /etc/init.d/dnsmasq, find the blob of lines that begin "procd_add_jail_mount" and add:
# procd_add_jail_mount /usr/local/bin/dnsmasq-nsupdate.sh /usr/bin/logger /usr/bin/nsupdate /lib /usr/lib /etc/dnsmasq-nsupdate
# restart dnsmasq
# Borrowed from https://askubuntu.com/questions/557098/bash-command-to-convert-ip-addresses-into-their-reverse-form
reverseip () {
local IFS
IFS=.
set -- $1
echo $4.$3.$2.$1
}
# Add, old, or del, determines mode
AOD=$1
# Mac address of the lease
MAC=$2
# New IP address
IP_ADDR=$3
RIP=$(reverseip $3)
# Hostname
L_HOSTNAME=$4
FQDN=${L_HOSTNAME}.${DNSMASQ_DOMAIN}
TTL="3600"
KEYFILE=/etc/dnsmasq-nsupdate/keyfile.key
if [ $AOD == "add" ] || [ $AOD == "old" ]
then
echo "
update delete ${FQDN} A
update add ${FQDN} ${TTL} A ${IP_ADDR}
update delete ${RIP}.in-addr.arpa. PTR
update add ${RIP}.in-addr.arpa. $TTL PTR ${FQDN}
send
" | nsupdate -k $KEYFILE | logger -t dnsmasq-nsupdate
else
# clear old addresses only
echo "
update delete ${FQDN} A
update delete ${RIP}.in-addr.arpa. PTR
send
" | nsupdate -k $KEYFILE | logger -t dnsmasq-nsupdate
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment