I use dnsmasq as part of the DNSSEC Appliance at Casa Mens, and I had it use a Lua script to notify me when a new DHCP lease is issued. (You may recall that Simon Kelly integrated Lua into dnsmasq two years ago.) That’s all fine and dandy, but I often need to check back which lease was issued. I can do that by logging onto DAP to find the leases file, but that’s cumbersome.

If you’ve been following a bit of what I do, you’ll know that I’m very interested in all things MQTT. It occurred to me it would be trivial to have dnsmasq invoke a script which publishes a lease change to a particular topic; setting the retain flag on the message will allow me to quickly check back – I’ll get all messages published to topics I subscribe to:

For example:

mosquitto_sub -h hippo -v -t 'network/dhcp/#'
network/dhcp/01:0b:5e:0d:93:1a old 192.168.1.193 2013-10-21 12:35:57 (JPs-iPad)
network/dhcp/02:a8:fa:6a:e8:09 old 192.168.1.212 2013-10-21 13:11:41 (android-129ebaa88x00a987)
network/dhcp/00:0b:51:51:cf:02 old 192.168.1.217 2013-10-21 13:47:25 ()
network/dhcp/00:09:ac:50:7d:8c old 192.168.1.205 2013-10-21 13:47:25 (SonosZP)
...

I configure dnsmasq to launch a shell script on lease change in dhcp.conf:

dhcp-script=/usr/local/dap/etc/dnsmasq/mqttpub.sh

The script itself is trivial:

#!/bin/sh

op="${1:-op}"
mac="${2:-mac}"
ip="${3:-ip}"
hostname="${4}"

tstamp="`date '+%Y-%m-%d %H:%M:%S'`"

topic="network/dhcp/${mac}"
payload="${op} ${ip} ${tstamp} (${hostname})"

mosquitto_pub -h 192.168.1.10 -t "${topic}" -m "${payload}" -r

One thing to note is the -r switch which sets the retain flag which causes a correctly configured broker to retain (i.e. store) the last message on a per/topic basis persistently.

If I then use my own mqtt-osx-notifier I’ll be notified as leases are issued.

DHCP, DNS, dnsmasq, and MQTT :: 21 Oct 2013 :: e-mail