Playing around with the Ubiquiti UniFi Controller

 •  Filed under ubiquiti, unifi, controller, api, curl

I found that today would be a great day to finally get an access point from Ubiquiti to improve my WiFi signal at home, while my intention was to keep my old wireless router in use for the wired and routing/DHCP/DNS stuff. So I went to the store and bought the UniFi AP AC Lite. The performance of this thing is pretty good so far, although I might get another one soon to cover the upper floor with better speeds over 5G as well. The cool thing about the UniFi series from Ubiquiti is that you can have all your UniFi devices managed with their software appliance, which I installed on a Linux Hyper-V VM.

Using cURL to fiddle around with the UniFi controller

cURL is a the tool that I used to build and create HTTP(S) requests to the UniFi controller. Google Chrome, my browser, has a feature that makes it easy to replay requests by generating full cURL commands that one could use directly from a bash shell.

Logging into the UniFi Controller using cURL

Logging into the controller with cURL was one of the easiest things to do. The page that handles the login action is https://controller:8443/api/login and only takes POST requests. You can login and save your cookies aswell by using the following command:

curl 'https://controller:8443/api/login' --data-binary '{"username":"usernamegoeshere","password":"passwordgoeshere","strict":true}' --compressed --insecure -c cookies.txt  

If you get something returned that says OK, then you are successfully logged in!

Obtaining the network configuration
curl --insecure -b cookies.txt -c cookies.txt 'https://controller:8443/api/s/default/rest/networkconf' > output.json  

Or, if you want to pass the output to a JSON processor directly:

curl --insecure -b cookies.txt -c cookies.txt 'https://controller:8443/api/s/default/rest/networkconf' | jq '.'  
Get user groups
curl --insecure -b cookies.txt -c cookies.txt 'https://controller:8443/api/s/default/rest/usergroup' > output.json  
Get UniFi devices
curl --insecure -b cookies.txt -c cookies.txt 'https://controller:8443/api/s/default/stat/device' > output.json  
Get list of connected devices
curl --insecure -b cookies.txt -c cookies.txt 'https://controller:8443/api/s/default/stat/sta' > output.json  

There are probably more things to do with the API, but I will figure that out later. Funny is that Ubiquiti doesn't actually have any official documentation for their API available.