Skip to content

Instantly share code, notes, and snippets.

@jcconnell
Last active May 14, 2025 13:44
Show Gist options
  • Select an option

  • Save jcconnell/0ee6c9d5b25c572863e8ffa0a144e54b to your computer and use it in GitHub Desktop.

Select an option

Save jcconnell/0ee6c9d5b25c572863e8ffa0a144e54b to your computer and use it in GitHub Desktop.

Revisions

  1. jcconnell revised this gist Jul 28, 2018. 1 changed file with 1 addition and 39 deletions.
    40 changes: 1 addition & 39 deletions unifi_wifi.sh
    Original file line number Diff line number Diff line change
    @@ -1,40 +1,3 @@
    Last Updated: 7/28/18

    A bash script to enable, disable or check the status of a UniFi WiFi network.

    ## Requirements:
    - jq
    - curl

    ## User Config:
    - unifi_username: The username for your controller
    - unifi_password: The password for the user. Leave inside the single quotes.
    - unifi_controller: The URL where you access your controller.
    - wifi_id: The ID of the WiFi you'd like to control. Details for acquiring the id below.
    - cookie: Path to the cookie for curl. Make sure the user that runs this script has permissions to this directory/file.
    ## Details:
    - This script is run like the following where parameter can be one of the following (enable | disable | status):
    `./unifi_wifi.sh parameter`
    ## Getting your wifi_id:
    - Navigate to your controller and sign in.
    - Navigate to Settings > Wifi Networks.
    - Click Edit next to the SSID you'd like to control.
    - Copy the ID from the end of the URL.
    - In the following example, the ID is ( 000d00c0e0b0e00d00000000 ):
    `https://example:8443/manage/site/default/settings/wlans/00bd00a6e0000e9da2cde10c/edit/000d00c0e0b0e00d00000000`


    ## Use:
    - Create a new file on your machine. I named mine unifi_wifi.sh.
    - Copy the below contents into your file.
    - Save the file.
    - chmod +x unifi_wifi.sh (Insert your file name there).
    - Use like `./unifi_wifi.sh parameter`

    ## Code
    ``` bash
    #!/bin/bash

    unifi_username=USERNAME
    @@ -94,5 +57,4 @@ elif [ "$1" == "status" ]; then
    else
    echo "Must include command line parameter [enable|disable|status]."
    fi
    unifi_logout
    ```
    unifi_logout
  2. jcconnell created this gist Jul 28, 2018.
    98 changes: 98 additions & 0 deletions unifi_wifi.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,98 @@
    Last Updated: 7/28/18

    A bash script to enable, disable or check the status of a UniFi WiFi network.

    ## Requirements:
    - jq
    - curl

    ## User Config:
    - unifi_username: The username for your controller
    - unifi_password: The password for the user. Leave inside the single quotes.
    - unifi_controller: The URL where you access your controller.
    - wifi_id: The ID of the WiFi you'd like to control. Details for acquiring the id below.
    - cookie: Path to the cookie for curl. Make sure the user that runs this script has permissions to this directory/file.
    ## Details:
    - This script is run like the following where parameter can be one of the following (enable | disable | status):
    `./unifi_wifi.sh parameter`
    ## Getting your wifi_id:
    - Navigate to your controller and sign in.
    - Navigate to Settings > Wifi Networks.
    - Click Edit next to the SSID you'd like to control.
    - Copy the ID from the end of the URL.
    - In the following example, the ID is ( 000d00c0e0b0e00d00000000 ):
    `https://example:8443/manage/site/default/settings/wlans/00bd00a6e0000e9da2cde10c/edit/000d00c0e0b0e00d00000000`


    ## Use:
    - Create a new file on your machine. I named mine unifi_wifi.sh.
    - Copy the below contents into your file.
    - Save the file.
    - chmod +x unifi_wifi.sh (Insert your file name there).
    - Use like `./unifi_wifi.sh parameter`

    ## Code
    ``` bash
    #!/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
    ```