Last active
September 24, 2017 13:01
-
-
Save bartekkois/1d59cb2f334fddc67a696de26fdbca87 to your computer and use it in GitHub Desktop.
Shell script for restarting all Unifi APs for specified site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Shell script for restarting all Unifi APs for specified site | |
# Usage: unifi_count_site username password baseurl site | |
# Requirements: shell, jq (https://stedolan.github.io/jq/) | |
# | |
username=$1 | |
password=$2 | |
baseurl=$3 | |
site=$4 | |
cookie=$(mktemp) | |
curl_cmd="curl --tlsv1 --silent --cookie ${cookie} --cookie-jar ${cookie} --insecure " | |
# Check arguments | |
unifi_requires() { | |
if [ -z "$username" -o -z "$password" -o -z "$baseurl" -o -z "$site" ] ; then | |
echo "Usage! unifi_count_site username password baseurl site" | |
return | |
fi | |
} | |
# Authenticate against unifi controller | |
unifi_login() { | |
${curl_cmd} --data "{'username':'$username', 'password':'$password'}" $baseurl/api/login >/dev/null 2>/dev/null | |
} | |
# Logout | |
unifi_logout() { | |
${curl_cmd} $baseurl/logout | |
} | |
# Restart APs | |
unifi_restart_site_ap() { | |
for i in `${curl_cmd} --data "json={}" $baseurl/api/s/$site/stat/device | jq '.data[].mac'` | |
do | |
mac=`echo $i | sed -e 's/^"//' -e 's/"$//'` | |
${curl_cmd} --data "json={'cmd':'restart', 'mac':'${mac}'}" $baseurl/api/s/$site/cmd/devmgr >/dev/null 2>/dev/null | |
sleep 5 | |
done | |
} | |
unifi_requires | |
unifi_login | |
unifi_restart_site_ap | |
unifi_logout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment