Get notified on Discord about UniFi devices with available firmware upgrade

This script uses your UniFi controller API to get firmware available upgrades and push notification for each device on Discord. It can be run as a daily cron job and adapted to any notification app.

Get notified on Discord about UniFi devices with available firmware upgrade

Checking the UniFi controller by hand for firmware updates is easy to forget, so this script does it for you and drops a clean notification straight into Discord whenever a device has an upgrade waiting.

Prerequisites: sudo apt-get install jq

#!/bin/bash
# ==============================================================================
# Title       : unifi-list-upgrades.sh
# Description : Call UniFi API to get firmware available upgrades 
# Author      : https://thelazyfox.xyz
# ==============================================================================

URL="https://ip:port"
USER=""
PASSWORD=""

API_LOGIN="/api/login"
API_DEVICES="/api/s/default/stat/device"

COOKIE_FILE="cookie.txt"
CURL_FILE_LOGIN="curl_login.txt"

PUSH_SCRIPT="../push/discord"
WEBHOOK="https://discord.com/api/webhooks/xxx"

wget https://raw.githubusercontent.com/ChaoticWeg/discord.sh/master/discord.sh  -O $PUSH_SCRIPT --quiet
chmod +x $PUSH_SCRIPT

echo "-- Login to API"
curl -c "$COOKIE_FILE" -X POST --data '{"username": "'${USER}'", "password":"'${PASSWORD}'", "remember":"true"}' "${URL}${API_LOGIN}" -o "$CURL_FILE_LOGIN" --insecure --silent > /dev/null

if grep -q "csrf_token" "$COOKIE_FILE"; then

    echo "-- Login success"

    response=$(curl -X POST -H 'Content-Type: multipart/form-data' -b "$COOKIE_FILE" "${URL}${API_DEVICES}" --insecure --silent)
 
    if [[ ! -z ${response} ]]; then
        echo "-- Getting information"
        for device in $(echo "${response}" | jq -r '.data[] | @base64'); do
            name=$(echo ${device} | base64 --decode | jq -r ".name")
            upgradable=$(echo ${device} | base64 --decode | jq -r ".upgradable")
            echo "-- -- ${name}: ${upgradable}"
            if [[ ${upgradable} == "true" ]]; then
                "${PUSH_SCRIPT}" \
                 --webhook-url="$WEBHOOK" \
                 --title "Firmware upgrade available!" \
                 --description "$name can be upgraded! \n\r ${URL}"
            fi
        done
    else
        echo "-- ERROR: Call failed"
    fi
    
else
    echo "-- ERROR: Login failed or Cookie not found"
fi

rm "${COOKIE_FILE}"
rm "${CURL_FILE_LOGIN}"

Drop it in a daily cron job and you will know about new firmware the same day it becomes available, without ever opening the controller yourself.

Looking for help?
If you are looking for some help or want a quick chat, please head over to the Discord Community!