(dnsmasq) dhcp-user-script not executed

I'm aware the latest verison of dnsmasq doesn't execute the script.
I had a script that detected a new dhcp release and send an email when the MAC address was not in a list.
Do anyone has a script that do something similiar, however not using dhcp-user-script?

#!/bin/sh

# script to detect new dhcp lease

# this will be called by dnsmasq everytime a new device is connected
# with the following arguments
# $1 = add | old
# $2 = mac address
# $3 = ip address
# $4 = device name

known_mac_addr="/etc/known_mac_addr"
notification_email="aaaa@gmail.com"
# check if the mac is in known devices list
grep -q "$2" "$known_mac_addr"
unknown_mac_addr=$?

if [ "$unknown_mac_addr" != 0 ]; then
    if [ "$1" == "add" ] ; then
	msg="New device on `uci get system.@system[0].hostname`.`uci get dhcp.@dnsmasq[0].domain` $*"
	echo `date` $msg >> /root/unknown_macs
	# encode colon (:) and send email
	sed -i 's/:/-/g' "$msg"
	echo -e "From: \nTo:$notification_email \nSubject:DHCP\n\n$msg" | msmtp "$notification_email"
	fi
fi

exit 0

You can create a hotplug script (in /etc/hotplug.d/dhcp)

[ "$ACTION" == "add" ] || exit 0

known_mac_addr="/etc/known_mac_addr"
notification_email="aaaa@gmail.com"

if ! /bin/grep -iq "$MACADDR" "$known_mac_addr"; then
        msg="IP $IPADDR assigned to a new device with MAC addr $MACADDR"
        echo -e "From: \nTo:$notification_email \nSubject:DHCP\n\n$msg" | msmtp "$notification_email"
fi
exit 0
5 Likes

That's wonderful.
Thank you so much!

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.