DEV Community

Cover image for Monitor Connected Devices to an ASUS Router Using Raspberry Pi
Eana Hufwe
Eana Hufwe

Posted on • Originally published at blog.1a23.com on

1

Monitor Connected Devices to an ASUS Router Using Raspberry Pi

Title should have explained it all. A simple Python script to monitor if a certain device has connected to the router via Wi-Fi, and send notifications accordingly. You can use this script for whatever purpose you want, though probably you might not be able to find one like most of others.

To use this script, you need SSH access to the router, something in your LAN that is always running (in my case, a Raspberry Pi), and the list of MAC addresses to monitor. In this example, I am using an ASUS RT-AC1200GU as the router. Other brand or make might need a different command.

First of all, SSH access of the router is needed to be enabled. In the router admin panel, go to Advanced Settings → Administration → Service → Enable SSH, select Yes. Put your Raspberry Pi SSH public key into the Authorized Keys box. Then click Apply at the bottom.

Screenshot of the router admin panel

The key point of this script is to get a list of connected clients via the ARP table of the router. Through testing, the method has been working pretty well, and almost no delay in reflecting the connectivity of devices.

ssh admin@192.168.1.1 /sbin/arp -n
? (192.168.1.2) at 00:11:22:33:44:55 [ether] on br0
? (192.168.1.3) at 66:77:88:99:aa:bb [ether] on br0
? (192.168.1.5) at cc:dd:ee:ff:00:11 [ether] on br0
? (192.168.1.7) at 22:33:44:55:66:77 [ether] on br0
? (192.168.1.9) at 88:99:aa:bb:cc:dd [ether] on br0
Enter fullscreen mode Exit fullscreen mode

Here you can see a list of devices with their IP and MAC address. We are using MAC address in the script so as to avoid change of IP in case of collision. MAC addresses of your target devices are easy to get from the status page of the router’s admin panel.

This script, written in Python, is ran every 10 minutes through Cron job on my Raspberry Pi. It only notifies when my own device is in the LAN and all devices to monitor are not. It will also reject to run when it cannot see the MAC address of itself is not seen in the list. (This would happen in cases like when the router is down.)

In the script I have used IFTTT Webhook for the notification. That’s because it is something easily enough to trigger in a script, but rarely used by me so that I can whitelist it from my Do Not Disturb mode. You can simply replace line 38 with anything service you want to use.

import subprocess
import requests
# MAC address of device running this script
self_mac = "00:11:22:33:44:55"
# MAC address of devices to check
to_monitor = {"66:77:88:99:aa:bb", "cc:dd:ee:ff:00:11"}
# MAC address of device which must be in the LAN for notifications to be sent
i_mac = "22:33:44:55:66:77"
# Path to the status file
record = "/etc/wlan_mon_status"
# Content of the status file
NOTIFIED_FLAG = "notified"
# Read in status file
with open(record) as f:
data = f.read()
# Output of ARP table check
out = subprocess.getoutput('ssh admin@192.168.1.1 /sbin/arp -n').lower()
if self_mac not in out:
# Error occurred while connecting to router
print("err", out)
exit(1)
if all(i not in out for i in to_monitor):
# All devices to check is disconnected
if NOTIFIED_FLAG not in data:
if i_mac in out:
# only notify if my device is still connected
# Send notification to IFTTT webhook
requests.post("https://maker.ifttt.com/trigger/wlan_monitor/with/key/abcdefghijklmnopqrst")
# write flag
with open(record, "w") as f:
f.write(NOTIFIED_FLAG)
else:
# At least one device to check is still connecting
if NOTIFIED_FLAG in data:
# clear flag
with open(record, "w") as f:
f.write("")
exit(0)
view raw wlan_monitor.py hosted with ❤ by GitHub

IFTTT activity config.

If you want to directly use this script, there are something that you need to add or change:

  • All the MAC addresses at the beginning;
  • IP address of your router;
  • The credentials of IFTTT Maker Webhook (if you want to use IFTTT);
  • Ensure that the status path is writable by the script;
  • Install requests;
  • Setup a cron job to run the script.

As simple as that.

Depending on your router brand and make, ARP table might not be updated as quickly as needed, in which case you may need to find another way to get the device list.

The post Monitor Connected Devices to an ASUS Router Using Raspberry Pi appeared first on 1A23 Blog.

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay