Dnsmasq-lease-sync.sh bug

There is a bug in the dnsmasq-lease-sync.sh script published in the following page:

the last line:
mv /tmp/dhcp_lease_new /tmp/dhcp.leases

replaces the file, but dnsmasq then fails to update new leases because apparently it maintains an open handle on the deleted file

the solution is to overwrite the file content (which maintains the same file handle, so that dnsmasq continues to update it) as follows:

cat /tmp/dhcp_lease_new > /tmp/dhcp.leases
rm /tmp/dhcp_lease_new

As an improvement on the above script I wrote the following that supports multiple dhcp instances that are necessary in case we have more subnets:

#!/bin/sh
# SYNCS contents of dnsmasq dhcp leases

OTHER_ROUTER=my-other-router
BASE_LEASES="/var/dhcp/*"

LEASES=$(echo ${BASE_LEASES})

for i in ${LEASES}; do
	NET_LEASE=$(basename $i)
	#echo ${NET_LEASE}
	scp -q root@${OTHER_ROUTER}:$i /tmp/${NET_LEASE}.${OTHER_ROUTER}
	cat /tmp/${NET_LEASE}.${OTHER_ROUTER} $i | sort -u > /tmp/${NET_LEASE}.new
	cat /tmp/${NET_LEASE}.new > $i
	rm -f /tmp/${NET_LEASE}.*
done
1 Like

Applying for OpenWrt wiki account

1 Like

Ok, but then you're accumulating a lot of duplicates by simply adding stuff to the lease file. Maybe do the traditional killall -HUP dnsmasq after the mv to force the update??? I haven't tried this specifically in that article's context, but I do build dynamic dnsmasq configs all the time and use killall frequently (I believe it's also used by adblock and friends).

Hello,

Thanks @ezplanet,

This modification did it ! DHCP leases are now well synchronised/