dhcp-script=/etc/detect_new_device.sh
Reference:
- https://wiki.openwrt.org/doc/howto/smtp.client
- https://fixmynix.com/send-mail-command-line-linux-openwrt/
- http://lifehacker.com/5506326/how-can-i-send-an-email-via-text-message
#!/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
notification_email="[email protected]"
if [ "$1" == "add" ]; then
msg="New device on `uci get system.@system[0].hostname`.`uci get dhcp.@dnsmasq[0].domain` $*"
echo `date` $msg >> /tmp/dhcpmasq.log
# encode colon (:) and send email
echo $msg | sed s/:/-/g | sendmail "$notification_email"
fi
This script only sends alerts if the mac address is not in the list
#!/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="[email protected]"
# check if the mac is in known devices list
grep -q "$2" "$known_mac_addr"
unknown_mac_addr=$?
if [ "$1" == "add" ] && [ "$unknown_mac_addr" -ne 0 ]; then
msg="New device on `uci get system.@system[0].hostname`.`uci get dhcp.@dnsmasq[0].domain` $*"
echo `date` $msg >> /tmp/dhcpmasq.log
# encode colon (:) and send email
echo $msg | sed s/:/-/g | sendmail "$notification_email"
fi
When a new device is added, dnsmasq calls detect_new_device.sh
with arguments add mac_addr ip_addr devicename
. The script checks if the device is new (if the dhcp lease hasn't expired, it calls with old
), then logs and emails (which eventually is a text message) the information.
I upgrade my openwrt to 23, the dhcp script breaks. I figure it out and finally make it works again. Share my procedure to help others.
This is my dhcp script:
After I add it to config with uci set, the openwrt log shows:
There are much logs like that. So I check the dnsmasq service script, thinking maybe the ujail restrict the command and file access, and the I modify the
/etc/init.d/dnsmasq
file.I update the
DHCPSCRIPT_DEPENDS
to add the command and file needed in dhcp script. After that, everything works, and error log has gone.It's a little nasty, if someone knows there is a more elegant way to change the
DHCPSCRIPT_DEPENDS
, please share.