-
-
Save jcconnell/0ee6c9d5b25c572863e8ffa0a144e54b to your computer and use it in GitHub Desktop.
#!/bin/bash | |
unifi_username=USERNAME | |
unifi_password='PASSWORD' | |
unifi_controller=https://EXAMPLE.COM:8443 | |
wifi_id=YOUR_WIFI_ID | |
cookie=/tmp/cookie | |
curl_cmd="curl -s -S --cookie ${cookie} --cookie-jar ${cookie} --insecure " | |
unifi_login() { | |
# authenticate against unifi controller | |
# Mute response by adding > /dev/null | |
${curl_cmd} -H "Content-Type: application/json" -X POST -d "{\"password\":\"$unifi_password\",\"username\":\"$unifi_username\"}" $unifi_controller/api/login > /dev/null | |
} | |
unifi_logout() { | |
# logout | |
${curl_cmd} $unifi_controller/logout | |
} | |
enable_wifi() { | |
# enables guest wifi network | |
# Mute response by adding > /dev/null | |
${curl_cmd} "$unifi_controller"'/api/s/default/rest/wlanconf/'"$wifi_id" -X PUT --data-binary '{"_id":"'"$site_id"'","enabled":true}' --compressed > /dev/null | |
} | |
disable_wifi() { | |
# enables guest wifi network | |
# Mute response by adding > /dev/null | |
${curl_cmd} "$unifi_controller"'/api/s/default/rest/wlanconf/'"$wifi_id" -X PUT --data-binary '{"_id":"'"$site_id"'","enabled":false}' --compressed > /dev/null | |
} | |
check_status() { | |
# checks wifi network status | |
# Mute response by adding > /dev/null | |
response=$(${curl_cmd} "$unifi_controller"'/api/s/default/rest/wlanconf/'"$wifi_id" --compressed) | |
status=$(echo $response | jq ".data[0].enabled") | |
if [ "$status" == "true" ]; then | |
exit 0 | |
elif [ "$status" == "false" ]; then | |
exit 1 | |
else | |
echo exit -1 | |
fi | |
} | |
unifi_login | |
if [ "$1" == "enable" ]; then | |
echo "Enabling WiFi." | |
enable_wifi | |
elif [ "$1" == "disable" ]; then | |
echo "Disabling WiFi." | |
disable_wifi | |
elif [ "$1" == "status" ]; then | |
check_status | |
else | |
echo "Must include command line parameter [enable|disable|status]." | |
fi | |
unifi_logout |
Something I came across while adding this is it only works as-written if you use want to control a network on the default site. If you use a different site, you will need to also grab that out of the same URL you use to find the network ID.
When you click edit on a wireless network in the Unifi Controller, it will look something like this in the new settings page:
/manage/site/<site_name>/v2/settings/wifi/network/form/<network_id>
You'll need to replace default
in the API endpoint URLs with site_name
from the URL above, otherwise you'll get a api.err.NoSiteContext
error.
Hope this helps.
Thanks for this. Very handy!
After reading a bit more and also the API I was fascinated with controlling the LED of my APs.
In the UI it can be turned on and off and recently even change color and brightness.
But I don't see how to do this in the API.
Any thoughts?
Cheers!
Hi,
Tried installing your script using of course the latest version of the controller.
Getting this error:
./unifi_wifi.sh status
parse error: Invalid numeric literal at line 1, column 10
exit -1
<!doctype html>
<script type="text/javascript" src="/vendor.0306759b.chunk.js"></script><script type="text/javascript" src="/main.d8e6c715.js"></script>May I ask for help?