Last active
March 21, 2020 08:35
-
-
Save blueset/2a648c1a0d96a4118972f425c273afaf to your computer and use it in GitHub Desktop.
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [email protected] /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