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 want1I used it to monitor whether my parent has left home when I am “seemingly asleep”. , 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.
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 email@example.com /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
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.
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;
- 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.