Skip to content

Instantly share code, notes, and snippets.

@shadowandy
Last active September 13, 2025 06:14
Show Gist options
  • Select an option

  • Save shadowandy/bab7013eaca45f862bebb44593e635a2 to your computer and use it in GitHub Desktop.

Select an option

Save shadowandy/bab7013eaca45f862bebb44593e635a2 to your computer and use it in GitHub Desktop.
Bash Script to reboot Unifi Devices

Creating the APIKEY to interact with your Unifi Controller

  1. Using a web browser, go to your Unifi Controller web portal
  2. Navigate to Control Plane > Integration
  3. Create a new APIKEY and note it down APIKEY_VALUE

Preparing the details

Ensure you have the:

  • Unifi Controller Address (CONTROLLER_IP_ADDRESS)
  • APIKEY created in the previous section (APIKEY_VALUE)
  • Name of the site (SITENAME_VALUE). Usually it is Default
  • Name of the managed device that you wish to reboot (DEVICENAME_VALUE). For example, U6+, UAP-AP-M

The Bash script

  • Ensure you have jq installed to manipulate JSON contents

Content of unifi_device_reboot.sh

#!/bin/sh

# Update the corresponding values in [..] without the [] like CONTROLLER='192.168.1.1'
CONTROLLER='[CONTROLLER_IP_ADDRESS]'
APIKEY='[APIKEY_VALUE]'
SITENAME='[SITENAME_VALUE]'
DEVICENAME='[DEVICENAME_VALUE]'

### Leave the following untouched ###

# Getting the Site ID
SITEID=$(curl -s -S -k -X GET "https://${CONTROLLER}/proxy/network/integration/v1/sites" -H "X-API-KEY: ${APIKEY}" -H 'Accept: application/json' | jq -r ".data[] | select(.name == \"${SITENAME}\") | .id")
echo "Site ID: ${SITEID}"

# Getting Device ID
DEVICEID=$(curl -s -S -k -X GET "https://${CONTROLLER}/proxy/network/integration/v1/sites/${SITEID}/devices" -H "X-API-KEY: ${APIKEY}" -H 'Accept: application/json' | jq -r ".data[] | select(.name == \"${DEVICENAME}\") | .id")
echo "Device ID for \"${DEVICENAME}\": ${DEVICEID}"

# Rebooting the Device
echo "Rebooting \"${DEVICEID}\""
curl -s -S -k -X POST "https://${CONTROLLER}/proxy/network/integration/v1/sites/${SITEID}/devices/${DEVICEID}/actions" -d '{"action":"RESTART"}' -H 'Content-Type: application/json' -H "X-API-KEY: ${APIKEY}" -H 'Accept: application/json'

Allow script to execute and schedule it to run

$ chmod +x unifi_device_reboot.sh

Schedule it using crontab

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