Skip to content

Instantly share code, notes, and snippets.

@jcconnell
Last active November 8, 2024 10:39
Show Gist options
  • Save jcconnell/0ee6c9d5b25c572863e8ffa0a144e54b to your computer and use it in GitHub Desktop.
Save jcconnell/0ee6c9d5b25c572863e8ffa0a144e54b to your computer and use it in GitHub Desktop.
A bash script to enable, disable or check the status of a UniFi WiFi network.
#!/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
@knyckis
Copy link

knyckis commented Nov 8, 2024

Redid this process today and can confirm that it still worked like a charm! The only thing different per the instructions above is that in finding the wifi id, you now click on the wifi network in question (thus getting to the edit page) and then read off the wifi_id directly from the slightly different url. Reusing the example id:

https://example:8443/manage/default/settings/wifi/000d00c0e0b0e00d00000000

So same same, fundamentally speaking, but looking at little different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment