DEV Community

Cover image for How to get notified of newly connected devices on your OpenWRT router
Brandon McFarlin
Brandon McFarlin

Posted on

How to get notified of newly connected devices on your OpenWRT router

So you’ve just set up OpenWRT with all the bells and whistles only to realize there is no out-of-the-box way to receive notifications for newly connected devices. No worries! With this tutorial, we will set up our OpenWRT server to send notifications to Pushover whenever a new device is connected to the server.


Let’s start with Pushover. Sign up is really easy, and pricing is very reasonable. It’s typically a one-time purchase per device used. For myself, I’ve purchased it for my iPhone and really enjoy its simplicity. Once signed up, create a new application with your preferred options. Lastly, find your app’s api key along with your user key. They should look something like this:

API Key
User Key

With pushover ready to go, let’s head back to our OpenWRT server. Create a new script file called new_device_notification.sh with the following lines:

#!/bin/sh

cat << "EOF" > /etc/hotplug.d/dhcp/90-newdev
[ "$ACTION" == "add" ] || exit 0
# [ "$ACTION" == "add" -o "$ACTION" == "update" ] || exit 0
known_macs="/etc/known_macs"
user_key="your-user-key"
api_key="your-api-key"
if ! /bin/grep -iq "$MACADDR" "$known_macs"; then
  msg="New device detected:
MAC: $MACADDR
IP: $IPADDR
Hostname: $HOSTNAME"
  echo "$MACADDR $IPADDR $HOSTNAME" >> /etc/known_macs
  curl -s \
       --form-string "token=$api_key" \
       --form-string "user=$user_key" \
       --form-string "title=New Device" \
       --form-string "message=$msg" \
       https://api.pushover.net/1/messages.json
fi
exit 0
EOF
Enter fullscreen mode Exit fullscreen mode

Replace your-api-key and your-user-key with the values provided from Pushover. This script will check for new devices on your DHCP server as these devices make connections. If the server has not seen it before, it will add the device to a list of known devices and send you a notification.

Finally, let’s make this script runnable and execute it:

chmod +x new_device_notification.sh
./new_device_notification.sh
Enter fullscreen mode Exit fullscreen mode

And that’s it! Simply restart your server and you should begin receiving messages from Pushover. You may receive many messages at the beginning as existing devices are added, but from then on, only new devices will trigger a message.

Enjoy!

Top comments (1)

Collapse
 
panchajanya1999 profile image
Panchajanya Sarkar

Thank You for this article!! Check out this guide of mine on how to setup a VPN with NextDNS over a cloud platform. I am planning to bring it on OpenWRT soon.

Link dev.to/panchajanya1999/setting-up-...