Skip to content

Instantly share code, notes, and snippets.

@TheQue42
Forked from shbatm/purge_clients.py
Created February 29, 2020 08:31
Show Gist options
  • Save TheQue42/cae49fd736a331e846a50d4ad9924dd0 to your computer and use it in GitHub Desktop.
Save TheQue42/cae49fd736a331e846a50d4ad9924dd0 to your computer and use it in GitHub Desktop.
Purge Junk Unifi Clients
#!/usr/bin/env python3
import requests
from urllib.parse import urljoin
username = "REDACTED"
password = r"REDACTED"
cloud_key_ip = "192.168.1.2"
controller_port = 8443
site_name = "default"
base_url = "https://{cloud_key_ip}:{controller_port}".format(
cloud_key_ip=cloud_key_ip, controller_port=controller_port
)
# How many do you have to forget?
#
# The API call is a **POST** to `/api/s/{site}/cmd/stamgr` with the body `{"macs":["00:1e:35:ff:ff:ff"],"cmd":"forget-sta"}` yes, it does look like you could submit them all in bulk to the API but the webUI doesn't expose that
#
# To fetch the list of all devices in json **GET** `/api/s/{site}/stat/alluser`
#
# Shouldn't be that hard to throw something together in python.
def api_login(sess, base_url):
payload = {"username": username, "password": password}
url = urljoin(base_url, "/api/login")
resp = sess.post(url, json=payload, headers={"Referer": "/login"})
if resp.status_code == 200:
print("[*] successfully logged in")
return True
else:
print("[!] failed to login with provided credentials")
return False
def api_get_clients(sess, base_url, site_name):
url = urljoin(
base_url, "/api/s/{site_name}/stat/alluser".format(site_name=site_name)
)
resp = sess.get(url)
client_list = resp.json()["data"]
print("[*] retreived client list")
return client_list
def api_del_clients(sess, base_url, site_name, macs):
payload = {"cmd": "forget-sta", "macs": macs}
url = urljoin(base_url, "/api/s/{site_name}/cmd/stamgr".format(site_name=site_name))
resp = sess.post(url, json=payload)
client_list = resp.json()["data"]
print("[*] purge complete")
return client_list
def client_macs(client_list):
macs = []
for client in client_list:
if ("use_fixedip" not in client and
("tx_packets" in client and client["tx_packets"] == 0)
and ("rx_packets" in client and client["rx_packets"] == 0)
and "mac" in client
):
macs.append(client["mac"])
print("[*] {!s} clients identified for purge".format(len(macs)))
return macs
if __name__ == "__main__":
sess = requests.Session()
sess.verify = False
requests.packages.urllib3.disable_warnings()
success = api_login(sess=sess, base_url=base_url)
if success:
client_list = api_get_clients(sess=sess, base_url=base_url, site_name=site_name)
macs = client_macs(client_list=client_list)
api_del_clients(sess=sess, base_url=base_url, site_name=site_name, macs=macs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment