IP Range variable info on new device detection script

Hi, i'm using a script to get an email when a new client connect to my router at home [https://gist.github.com/jwalanta/53f55d03fcf5265938b64ffd361502d5], it works but if i wanna add the info of what network the client is connected and if i use " iot="10.2.20.* " i don't get a notification but if i change to " iot="10.2.20.22 " and force the device to get that ip the scripts works.

How can i cover a full range of ip's?

Thanks

known_mac_addr="/etc/config/dhcp"
notification_email="abc@123.com"
lan="10.2.1.*"
www="10.2.10.*"
iot="10.2.20.*"
work="10.2.30.*"
kids="10.2.40.*"
guest="10.2.50.*"

#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" ] && [ "$3" == "$iot" ] && [ "$unknown_mac_addr" -ne 0 ]; then
        (
                echo "Subject: ALERTA DE NOVO DISPOSITIVO NA REDE"
                echo "To: $notification_email"
                echo ""
                echo "Evento:           Novo Dispositivo em $(uci get system.@system[0].hostname)"
                echo "Data:             $(date '+%d/%m/%Y') ás $(date +%X)"
                echo "MAC:              $mac"
                echo "IP:               $3"
                echo "REDE:             X83-IoT"

Basically you can't use [ $xx = value ] to match against wildcards. The only way to that within OpenWrt's shell is to use case in ...) ...;; esac expressions.

if [ "$1" = "add" ]  && [ "$unknown_mac_addr" -ne 0 ]; then
  case "$3" in
    $iot)
      echo "Is an IoT client"
    ;;
    $lan)
      echo "Is a LAN client"
    ;;
    $www)
      echo "Is a WWW client"
    ;;
    $work)
      echo "Is a work client"
    ;;
    $kids)
      echo "Is a kids client"
    ;;
    $guest)
      echo "Is a guest client"
    ;;
  esac
fi
1 Like

Many thanks @jow , that's it, working 100% now.

#!/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="123@mail.com"


#Vlans
lan="10.2.1.*"
www="10.2.10.*"
iot="10.2.20.*"
wrk="10.2.30.*"
kid="10.2.40.*"
gst="10.2.50.*"


#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: ALERTA DE NOVO DISPOSITIVO NA REDE"
		echo "To: $notification_email"
		echo ""
		echo "Evento:           Novo Dispositivo em $(uci get system.@system[0].hostname)"
		echo "Data:             $(date '+%d/%m/%Y') ás $(date +%X)"
		echo "Nome:             $4"
		echo "MAC:              $mac"
		echo "IP:               $3"
  	case "$3" in
    $lan)
		echo "Rede:		Lan"
    ;;
    $www)
		echo "Rede:		WWW"
    ;; 
    $iot)
		echo "Rede:		IoT"
    ;;
    $wrk)
		echo "Rede:		Work"
    ;;
    $kid)
		echo "Rede:		Kids"
    ;;
    $gst)
		echo "Rede:		Guest"
    ;;
  esac
       ) | /usr/sbin/sendmail "$notification_email"
fi

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