Skip to content

Instantly share code, notes, and snippets.

@grimpy
Created October 18, 2019 17:20
Show Gist options
  • Save grimpy/abab17b2fb7f388c43109f1e4e04b92f to your computer and use it in GitHub Desktop.
Save grimpy/abab17b2fb7f388c43109f1e4e04b92f to your computer and use it in GitHub Desktop.
#!/usr/bin/micropython
import os
import ujson
import time
LIMIT = 100 * 1024 ** 2
def checklimits(macs):
proc = os.popen("nlbw -c json -g ip,mac")
data = ujson.loads(proc.read())
proc = os.popen("iptables -vnL FORWARD")
iptables = proc.read()
macindex = data['columns'].index('mac')
ipindex = data['columns'].index('ip')
rxindex = data['columns'].index('rx_bytes')
txindex = data['columns'].index('tx_bytes')
for row in data['data']:
mac = row[macindex]
if mac not in macs:
continue
ip = row[ipindex]
useddata = row[rxindex] + row[txindex]
print("Processing {} with mac {}".format(ip, mac))
if useddata > LIMIT:
print("Limit reached for {}".format(ip))
if ip not in iptables:
print("Adding filter")
os.popen("iptables -I FORWARD -s {} -j DROP".format(ip)).read()
elif ip in iptables:
print("Removing filter for {}".format(ip))
os.popen("iptables -D FORWARD -s {} -j DROP".format(ip)).read()
if __name__ == '__main__':
macs = ["30:24:32:a9:f8:49", "e0:2c:b2:57:45:64"]
checklimits(macs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment