Skip to content

Instantly share code, notes, and snippets.

@blueset

blueset/wlan_monitor.py

Last active Mar 21, 2020
Embed
What would you like to do?
Monitor Connected Devices to an ASUS Router Using Raspberry Pi https://blog.1a23.com/2020/03/03/monitor-connected-devices-to-an-asus-router-using-raspberry-pi/
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment