Table of Contents
How to control a Unifi network with Python
Using Python library unificontrol
:
The unificontrol API — unificontrol 0.2.9 documentation
readthedocs.io
on Github:
GitHub - nickovs/unificontrol: A high-level Python interface to the Unifi controller software
github.com
Base script
from unificontrol import UnifiClient
client = UnifiClient(
host="host_name",
username="unifi_username",
password="unifi_password",
)
devices = client.list_devices()
# pp.pprint(devices)
print()
for device in devices:
print(device['name'])
print(" model: ", device['model'])
print(" ip: ", device['ip'])
print(" mac: ", device['mac'])
print(f" uptime: {round((device['_uptime'] / 60 / 60 / 24), 1)} days")
print(" version: ", device['version'])
print(" required_version: ", device['required_version'])
print(" connected_at: ", datetime.utcfromtimestamp(device['connected_at']))
if 'gateway_mac' in device:
print(" gateway_mac: ", device['gateway_mac'])
print()
Restart a single device
### RESTART DEVICE
client.restart_ap("device_mac_address")
Restart all devices
### RESTART ALL DEVICES
devices = client.list_devices()
for device in devices:
mac = device['mac']
print(f"Restarting {mac}")
client.restart_ap(mac)
Upgrade a device
### UPGRADE DEVICE
client.upgrade_device("device_mac_address")