Skip to content

Instantly share code, notes, and snippets.

@ksverdlov
Forked from joshspicer/boot_alert.sh
Last active November 1, 2024 19:04
Show Gist options
  • Save ksverdlov/33a26fed4c4413171171de86ba59c74f to your computer and use it in GitHub Desktop.
Save ksverdlov/33a26fed4c4413171171de86ba59c74f 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 after OpenWRT reboot, start monitoring and list of currently connected wireless devices.
# write-up at: spcr.me/openwrt-alert
# Some modifications: https://gist.github.com/ksverdlov/33a26fed4c4413171171de86ba59c74f
# ~ note ~
# Call from in /etc/rc.local
APIKEY=<YOUR_BOT_API_KEY_HERE>
CHATID_MUTED=<YOUR_MUTED_CHAT_ID_HERE>
TELEGRAM="https://api.telegram.org/bot$APIKEY/sendMessage"
TEXT="Router has rebooted"
TEXT="<b>Router has rebooted</b>"
msg=`/usr/bin/curl --data-urlencode "text=$TEXT" \
--data-urlencode "chat_id=$CHATID_MUTED" \
--data "parse_mode=HTML" \
"$TELEGRAM"`
# Avoid race condition
sleep 30
/usr/sbin/hostapd_cli -a /root/event_alert.sh -B -i wlan0
/usr/sbin/hostapd_cli -a /root/event_alert.sh -B -i wlan1
/root/manual_alert.sh
#!/bin/sh
# Alerts via telegram when a device connects or disconnects on OpenWRT's wireless interfaces
# write-up at: spcr.me/openwrt-alert
# Some modifications: https://gist.github.com/ksverdlov/33a26fed4c4413171171de86ba59c74f
# ~ note ~
# Depends on hostapd_cli
# KNOWN_MAC_LIST is a list of known mac addresses in format 'MAC<space>Description'. Like:
# aa:bb:cc:dd:ee:ff Amazon Echo Ivan
# hostapd_cli event vars
# $1 = interface
# $2 = action
# $3 = MAC Addr
if (echo $2 | grep "CONNECTED$\|DISCONNECTED$"); then
APIKEY=<YOUR_BOT_API_KEY_HERE>
CHATID_MUTED=<YOUR_MUTED_CHAT_ID_HERE>
CHATID_UNMUTED=<YOUR_UNMUTED_CHAT_ID_HERE>
TELEGRAM="https://api.telegram.org/bot$APIKEY/sendMessage"
KNOWN_MAC_LIST="/root/known_mac_list"
device_info=$(grep "^$3\s" $KNOWN_MAC_LIST | cut -f2- -d' ')
if [ ! -z "$device_info" ]; then
text="Known device <code>| $3 | $1 | $2 |</code> $device_info"
msg=$(/usr/bin/curl --data-urlencode "text=$text" \
--data-urlencode "chat_id=$CHATID_MUTED" \
--data "parse_mode=HTML" \
"$TELEGRAM")
continue
else
manufact=$(/usr/bin/curl "https://api.macvendors.com/$3")
text="Unknown device <code>| $3 | $1 | $2 |</code> $manufact"
msg=$(/usr/bin/curl --data-urlencode "text=$text" \
--data-urlencode "chat_id=$CHATID_UNMUTED" \
--data "parse_mode=HTML" \
"$TELEGRAM")
fi
fi
#!/bin/sh
# Alerts via telegram a list of devices connected to OpenWRT's wireless interfaces
# write-up at: spcr.me/openwrt-alert
# Some modifications: https://gist.github.com/ksverdlov/33a26fed4c4413171171de86ba59c74f
APIKEY=<YOUR_BOT_API_KEY_HERE>
CHATID_MUTED=<YOUR_MUTED_CHAT_ID_HERE>
CHATID_UNMUTED=<YOUR_UNMUTED_CHAT_ID_HERE>
TELEGRAM="https://api.telegram.org/bot$APIKEY/sendMessage"
KNOWN_MAC_LIST="/root/known_mac_list"
NL="
"
known_devices=""
unknown_devices=""
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 list of known MAC addresses
device_info=$(grep -i "^$mac\s" $KNOWN_MAC_LIST | cut -f2- -d' ')
if [ ! -z "$device_info" ]; then
known_devices="${known_devices}$NL<code>$interface | $mac |</code> $device_info"
continue
else
manufact=$(/usr/bin/curl "https://api.macvendors.com/$mac")
unknown_devices="${unknown_devices}$NL<code>$interface | $mac |</code> $manufact"
# delay for https://macvendors.com/api
sleep 2
fi
done
done
if [ ! -z "$known_devices" ]; then
header="Known devices currently connected:"
msg=$(/usr/bin/curl --data-urlencode "text=$header$known_devices" \
--data-urlencode "chat_id=$CHATID_MUTED" \
--data "parse_mode=HTML" \
"$TELEGRAM")
fi
if [ ! -z "$unknown_devices" ]; then
header="Unknown devices currently connected:"
msg=$(/usr/bin/curl --data-urlencode "text=$header$unknown_devices" \
--data-urlencode "chat_id=$CHATID_UNMUTED" \
--data "parse_mode=HTML" \
"$TELEGRAM")
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment