Skip to content

Instantly share code, notes, and snippets.

@fakhamatia
Last active June 9, 2025 13:44
Show Gist options
  • Save fakhamatia/6a5768df2cebdad58552821e160baaf9 to your computer and use it in GitHub Desktop.
Save fakhamatia/6a5768df2cebdad58552821e160baaf9 to your computer and use it in GitHub Desktop.

πŸ“‘ Device Notification Script Setup Guide (OpenWrt)

1. 🏭 Register for MAC Vendor Lookup

  • Go to macvendors.com
  • Sign up and generate your API token
  • Paste the token into the MACVENDORS_TOKEN variable inside the script

2. πŸ€– Create a Telegram Bot

  • Open @BotFather on Telegram
  • Create a new bot and get the BOT_TOKEN
  • Send a message to your bot and use a tool like IDBot or logs to find your CHAT_ID
  • Fill in both BOT_TOKEN and CHAT_ID in the script

3. πŸ’Ύ Save the Script

  • Save the script to the following path :
    /etc/hotplug.d/dhcp/99-notify_new_device
  • Use chmod:
    chmod +x /etc/hotplug.d/dhcp/99-notify_new_device

4. πŸ“’ Add Known MAC Addresses

  • Create a file at:
    /etc/known_macs
  • List known MAC addresses one per line
  • You can optionally add a name or description after the MAC address:
    50:98:39:EE:1D:93    Ali's iPhone
    3C:2E:F9:1A:44:55    Office Printer
    A4:7C:94:2F:18:01    Smart TV - Living Room
    

βœ… Done!

Now every time an unknown device connects to your network,
you'll get a detailed notification in Telegram πŸ“¬

#!/bin/sh
KNOWN_MACS="/etc/known_macs"
MACVENDORS_TOKEN=""
BOT_TOKEN=""
CHAT_ID=""
MAC="$MACADDR"
IP="$IPADDR"
HOSTNAME="$HOSTNAME"
[ -z "$MAC" ] && exit 0
grep -iq "$MAC" "$KNOWN_MACS" 2>/dev/null && exit 0
VENDOR=$(curl -s -G "https://api.macvendors.com/v1/lookup/${MAC}" \
-H "Authorization: Bearer ${MACVENDORS_TOKEN}" \
-H "Accept: text/plain")
if echo "$VENDOR" | grep -qE '^{.*}$' || [ -z "$VENDOR" ] || [ "$VENDOR" = "null" ]; then
VENDOR="Unknown"
fi
DATE=$(date '+%Y-%m-%d %H:%M:%S')
MESSAGE="🚨 <b>New Device Connected</b>
<b>πŸ•’ Time:</b> $DATE
<b>πŸ’» Hostname:</b> $HOSTNAME
<b>🌐 IP:</b> <code>$IP</code>
<b>πŸ” MAC:</b> <code>$MAC</code>
<b>🏭 Vendor:</b> $VENDOR"
curl -s -X POST https://api.telegram.org/bot$BOT_TOKEN/sendMessage \
-d chat_id=$CHAT_ID \
-d parse_mode="HTML" \
-d text="$MESSAGE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment