Skip to content

Instantly share code, notes, and snippets.

@joshspicer
Last active July 24, 2022 19:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save joshspicer/e09c3158074cdd584c79e2bb5bd4e640 to your computer and use it in GitHub Desktop.
Save joshspicer/e09c3158074cdd584c79e2bb5bd4e640 to your computer and use it in GitHub Desktop.
[OpenWrt] Telegram alert when a new wireless device associates with access point. (http://spcr.me/openwrt-alert)
#!/bin/sh
# Alerts via telegram when a new device joins OpenWRT wireless interface
# write-up at: spcr.me/openwrt-alert
#
# ~ note ~
# Call from in /etc/rc.local
TELEGRAM="https://api.telegram.org/bot<YOUR_API_KEY>/sendMessage?chat_id=<YOUR_CHAT_ID_HERE>&text="
REBOOTTEXT="Router has rebooted"
msg=`/usr/bin/curl "$TELEGRAM$REBOOTTEXT"`
# Avoid race condition
sleep 20
/usr/sbin/hostapd_cli -a/root/associated.sh -B -iwlan0
/usr/sbin/hostapd_cli -a/root/associated.sh -B -iwlan1
/usr/sbin/hostapd_cli -a/root/associated.sh -B -iwlan1-1
/root/manual_alert.sh
#!/bin/sh
# Alerts via telegram when a new device joins OpenWRT wireless interface
# write-up at: spcr.me/openwrt-alert
#
# ~ note ~
# Depends on hostapd_cli
TELEGRAM="https://api.telegram.org/bot<YOUR_API_KEY_HERE>/sendMessage?chat_id=<YOUR_CHAT_ID_HERE>&text="
# hotapd_cli event vars
# $1 = interface
# $2 = action
# $3 = MAC Addr
if grep "^$3$" /root/seen.txt
then
# echo "Have seen $3"
continue
else
# echo "Have not seen $3"
manufact=`/usr/bin/curl "https://api.macvendors.com/$3"`
TEXT="New Device $3 ($manufact) detected on interface $1"
msg=`/usr/bin/curl "$TELEGRAM$TEXT"`
echo "$3" >> /root/seen.txt
fi
#!/bin/sh
# Alerts via telegram when a new device joins OpenWRT wireless interface
# write-up at: spcr.me/openwrt-alert
#
TELEGRAM="https://api.telegram.org/bot<YOUR_API_KEY>/sendMessage?chat_id=<YOUR_CHAT_ID>&text="
REBOOTTEXT="Manual Scan"
msg=`/usr/bin/curl "$TELEGRAM$REBOOTTEXT"`
for interface in `iwinfo | grep ESSID | cut -f 1 -s -d" "`
do
macaddrs=`iwinfo $interface assoclist | grep dBm | cut -f 1 -s -d" "`
for mac in $macaddrs
do
# Ignore occasional bad output of cut
reqsubstr="freq"
if [ -z "${mac##*$reqsubstr*}" ] ;then
continue
fi
# Check against our log of "seen" MAC addresses
if grep -i "^$mac$" /root/seen.txt > /dev/null
then
echo "Have seen '$mac'"
continue
else
echo "Have not seen '$mac'"
manufact=`/usr/bin/curl "https://api.macvendors.com/$mac"`
TEXT="New device $mac ($manufact) manually detected on interface $interface"
msg2=`/usr/bin/curl "$TELEGRAM$TEXT"`
echo "$mac" >> /root/seen.txt
fi
done
done
@TeamYukizome
Copy link

Where's the associated.sh file?

@nsis17
Copy link

nsis17 commented Mar 24, 2022

hi ,

script not working

https://joshspicer.com/openwrt-alert

msg=/usr/bin/curl "$TELEGRAM$REBOOTTEXT"
curl: (3) Error

help pls

@ksverdlov
Copy link

hi ,

script not working

https://joshspicer.com/openwrt-alert

msg=/usr/bin/curl "$TELEGRAM$REBOOTTEXT" curl: (3) Error

help pls

Hello, you can check my fork with the issue fixed https://gist.github.com/ksverdlov/33a26fed4c4413171171de86ba59c74f

@ksverdlov
Copy link

Where's the associated.sh file?

I'm not the author, but I think that it should be /root/event_alert.sh

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