Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save erdoukki/a597ee80128d3a1719df87f14d6a5a9e to your computer and use it in GitHub Desktop.
Save erdoukki/a597ee80128d3a1719df87f14d6a5a9e to your computer and use it in GitHub Desktop.
Detect new network devices connecting to OpenWrt and send text message

Add the following line in /etc/dnsmasq.conf

dhcp-script=/etc/detect_new_device.sh

Setup sendmail to send email to your text number.

Reference:

Create /etc/detect_new_device.sh with the following content

#!/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="1234567890@txt.att.net"

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

Alternative script using whitelist

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="1234567890@txt.att.net"

# 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.

@erdoukki
Copy link
Author

My own version, using mstmp

/root/detect_new_device.sh

#!/bin/sh

# script to detect new dhcp lease

# HOW-TO
# - add in /etc/dnsmasq.conf:
## DNSMASQ - DETECT-NEW-DEVICES
# dhcp-script=/root/detect_new_device.sh
# - optionally add in /etc/sysupgrade.conf:
# # DNSMASQ - DETECT-NEW-DEVICES
# /root/detect_new_device.sh
# - enable execute bit with:
# $ chmod a+x /root/detect_new_device.sh

# 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/config/dhcp"
notification_email="user@acme"

#Convert MAC to uppercase
mac=$(echo "$2" | awk '{print toupper($0)}')

# check if the mac is in known devices list
grep -q "$mac" "$known_mac_addr"
unknown_mac_addr=$?

if [ "$1" == "add" ] && [ "$unknown_mac_addr" -ne 0 ]; then
	(
		echo "Subject: New device on $(uci get system.@system[0].hostname).$(uci get dhcp.@dnsmasq[0].domain)" 
		echo "To: $notification_email"
		echo ""
		echo "$(date):	New device on $(uci get system.@system[0].hostname).$(uci get dhcp.@dnsmasq[0].domain)" 
		echo "action:	$1"
		echo "mac:	$2" 
		echo "ip:	$3" 
		echo "name:	$4" 
	) | /usr/sbin/sendmail "$notification_email"
fi

TEST

root@LPM:~# /root/detect_new_device.sh add 123456

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment