Created
September 14, 2020 20:44
-
-
Save donnlee/00d9bcd7a6b1c5e761be3714d2c1c61a to your computer and use it in GitHub Desktop.
Clean-up Unifi controller bogus MAC addresses
This file contains hidden or 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
# In my particular case, there are bogus MACs that all end in "42:42:d7:59" (var SUFFIX_OF_SPAM) | |
# This script gets rid of those, but saves MACs in SAVE_THESE. | |
# https://github.com/finish06/pyunifi | |
# Create py virenv for unifi deps & activate it, then inside the virenv: | |
# pip3 install pyunifi | |
# Remove/"forget" an array of MACs, written in ruby: | |
# https://gist.github.com/rspeicher/fd931d557a35c88011e61fbe417a0020 | |
# Manager is 'stamgr' (station manager?) | |
# Found: Posting to the endpoint `api/s/{site}/cmd/<manager>` | |
# Command is 'forget-sta' (forget station?) | |
# Params: | |
# 'macs': [mac1, mac2, ...] | |
import os | |
import sys | |
from pyunifi.controller import Controller | |
username = os.environ['UNIFI_USERNAME'] | |
password = os.environ['UNIFI_PASSWORD'] | |
CHUNK_SIZE = 100 | |
STOP_THRESHOLD = 300 | |
SUFFIX_OF_SPAM = '42:42:d7:59' | |
SAVE_THESE = ['42:42:42:42:d7:59'] | |
c = Controller('172.16.0.201', username, password, ssl_verify=False) | |
def forget_chunk(c, qty_to_forget): | |
i = 0 | |
macs_to_forget = [] | |
users = c.get_users() | |
print('users (good & spam) remaining: {}'.format(len(users))) | |
if len(users) < STOP_THRESHOLD: | |
print('We cleaned enough. Stopping.') | |
sys.exit() | |
for user in users: | |
if i >= qty_to_forget: | |
break | |
if not user['is_wired']: | |
continue | |
mac = user['mac'] | |
if mac in SAVE_THESE: | |
continue | |
if mac.endswith(SUFFIX_OF_SPAM): | |
print('Going to forget: {}'.format(mac)) | |
macs_to_forget.append(mac) | |
i += 1 | |
print('MACs to forget: {}'.format(macs_to_forget)) | |
params = {'macs': macs_to_forget} | |
print('Requesting forget-sta via api...') | |
result = c._run_command('forget-sta', params=params) | |
print('Result of cmd: {}'.format(result)) | |
while True: | |
forget_chunk(c, CHUNK_SIZE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment