A Python script that uses the aiounifi library to set Power over Ethernet (PoE) status on specified ports of a UniFi switch to a desired state through the UniFi Network Controller.
- Set PoE Status: Set PoE on/off for individual ports or ranges of ports to a specific desired state
- Flexible Port Selection: Support for individual ports, ranges, and comma-separated lists
- Safety Features:
- User confirmation before making changes (can be bypassed with
--yes) - Verification of changes after execution
- Checks for PoE capability on each port
- User confirmation before making changes (can be bypassed with
- Python 3.8 or later
- UniFi Network Controller (tested with 9.2)
You will need a local user created in your UniFi OS Console to log in with. Ubiquiti SSO Cloud Users will not work.
-
Login to your Local Portal on your UniFi OS device, and select Users.
Note: This must be done from the UniFi OS by accessing it directly by IP address (i.e. Local Portal), not via unifi.ui.com or within the UniFi Network app. -
Go to Admins & Users from the left hand side menu or [IP address]/admins/users e.g. 192.168.1.1/admins/users.
-
Select Add New Admin.
-
Check Restrict to local access only and fill out the fields for your user. Select Full Management for Network. OS Settings are not used, so they can be set to None.
-
In the bottom right, select Add.
-
Clone or download this repository
-
Create virtual environment
python3 -m venv venv source ./venv/bin/activate -
Install dependencies (if not using the existing virtual environment):
pip install -r requirements.txt
python unifi_poe_toggle.py <controller_host> <username> <password> <switch_mac> <port_indexes> --state <on|off> [options]controller_host: IP address or hostname of your UniFi controllerusername: Username for UniFi controller loginpassword: Password for UniFi controller loginswitch_mac: MAC address of the target switch (any format:aa:bb:cc:dd:ee:ff,aa-bb-cc-dd-ee-ff, oraabbccddeeff)port_indexes: Port numbers to configure (see examples below for formats)
--state {on,off,enable,disable}: Desired PoE state (on/enable to turn on, off/disable to turn off)
--port PORT: Controller port (default: 443)--site SITE: Site name (default: "default")--verify-ssl: Verify SSL certificates (default: disabled for self-signed certs)--yes: Skip confirmation prompt (for automation)--debug: Enable debug logging--help: Show help message
The script supports flexible port specification:
- Individual ports:
1,3,5,8 - Ranges:
1-8(ports 1 through 8) - Mixed:
1,3,5-8,12(ports 1, 3, 5, 6, 7, 8, and 12)
Enable PoE on ports 1, 2, and 3:
python unifi_poe_toggle.py 192.168.1.1 admin mypassword 00:11:22:33:44:55 1,2,3 --state onDisable PoE on ports 1, 2, and 3:
python unifi_poe_toggle.py 192.168.1.1 admin mypassword 00:11:22:33:44:55 1,2,3 --state offEnable PoE on ports 1 through 8:
python unifi_poe_toggle.py 192.168.1.1 admin mypassword 00:11:22:33:44:55 1-8 --state onFor a controller on a non-standard port (like UniFi OS on port 443):
python unifi_poe_toggle.py 192.168.1.1 admin mypassword 00:11:22:33:44:55 5,10-12 --state off --port 443For a specific site (not the default):
python unifi_poe_toggle.py unifi.local admin mypassword aa:bb:cc:dd:ee:ff 1-24 --state on --site branch-officeEnable detailed logging for troubleshooting:
python unifi_poe_toggle.py 192.168.1.1 admin mypassword 00:11:22:33:44:55 1,2,3 --state on --debugSkip confirmation prompts for scripting and automation:
# For scripts and automation - no user interaction required
python unifi_poe_toggle.py 192.168.1.1 admin mypassword 00:11:22:33:44:55 1,2,3 --state off --yesWhen setting PoE states:
--state onor--state enable→ sets port to "auto" (enable PoE with automatic detection)--state offor--state disable→ sets port to "off" (disable PoE)
This script is provided as-is for educational and operational purposes. Use responsibly and test in non-production environments first.