Skip to content

Instantly share code, notes, and snippets.

@storbukas
Created February 5, 2018 19:22
Show Gist options
  • Save storbukas/5e5465ae7bab5d7138f9cfab8e0ff9c2 to your computer and use it in GitHub Desktop.
Save storbukas/5e5465ae7bab5d7138f9cfab8e0ff9c2 to your computer and use it in GitHub Desktop.
DD-WRT Unknown client notification
#!/bin/sh
#
# Details:
# Author: Lars Erik Storbukås <[email protected]>
# http://larserik.storbukas.no
# System: DD-WRT v3.0-r28628 std (12/29/15)
# File location: /jffs/ets/scripts/dhcp-lease-notifier.sh
#
# DHCP Lease Notifier
# ------------------------
# This script notifies about unknown clients upon DHCP lease. Any hosts
# connecting to the router is appended to a list of known clients. This
# means that you will only receive one notification per device (NIC).
#
# This will be called by DNSMasq every time a new device is connected
# (when configured) with the following arguments:
# $1 = add | old
# $2 = mac address
# $3 = ip address
# $4 = device name
#
# The following DNSMasq option must be added through the DD-WRT web interface,
# at Services > Services > DNSMasq > Additional DNSMasq Options:
# dhcp-script=/jffs/etc/scripts/dhcp-lease-notifier.sh
#
# Remember to make this script executable by issuing the following command:
# chmod +x /jffs/etc/scripts/dhcp-lease-notifier.sh
#
# The notification command uses a SMS Gateway to notify the network
# administrator about the unknown client.
#
# In order for this script to work properly, you must either replace the
# notification command, or provide an API key for https://gatewayapi.com.
# DNSMasq arguments
action="${1:-op}"
mac="${2:-mac}"
ip="${3:-ip}"
hostname="${4}"
# Details
known_mac_addr="/jffs/etc/scripts/known_mac_addr"
timestamp="`date '+%d-%m-%Y %H:%M:%S'`"
# SMS API Configuration
sms_api_key="<INSERT API KEY>" # API Key
sms_api_key_passwd="" # Normally empty
sms_sender="<INSERT SMS SENDER ID>" # Max 11 characters
sms_area_code="47" # Norway area code
sms_recipient="<INSERT SMS RECEIVER ID>" # Phone number of recipient
# Notification message
payload="A new device has connected to the network.
Mac: ${mac}
IP: ${ip}
Time: ${timestamp}
Hostname: ${hostname}"
# Search for the MAC-address in known_mac_addr-file
grep -q "$2" "$known_mac_addr"
unknown_mac_addr=$?
# Check if search found a match - if not, then notify
if [ "$unknown_mac_addr" -ne 0 ]; then
# Add MAC to known_mac_addr-file
echo $mac >> $known_mac_addr
# Notification command
curl -k -v https://gatewayapi.com/rest/mtsms \
-u ${sms_api_key}:${sms_api_key_passwd} \
-d sender="${sms_sender}" \
-d message="${payload}" \
-d recipients.0.msisdn=${sms_area_code}${sms_recipient}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment