Skip to content

Instantly share code, notes, and snippets.

@jvandijk
Last active June 26, 2022 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jvandijk/eddd021e0ee93f169dc77802e4331401 to your computer and use it in GitHub Desktop.
Save jvandijk/eddd021e0ee93f169dc77802e4331401 to your computer and use it in GitHub Desktop.
Multi device & multi request Unifi POE switching
#!/bin/bash
if [ $# -lt 2 ]; then
echo "Usage : $0 port-nr device"
exit
fi
if [ "$2" = "uap" ]; then
MAC=ma:co:fu:ap
else
MAC=ma:co:fu:sw
fi
DEVICE=/tmp/device-$2.json
COOKIE=/tmp/cookie.txt
if [ ! -f "$COOKIE" ]; then
curl -X POST --insecure -H "Content-Type: application/json" -d '{
"username": "user",
"password": "password",
"remember": true
}' -c $COOKIE -o /dev/null -s https://ip-cloudkey:port/api/login
fi
if [ ! -f "$DEVICE" ]; then
curl -X GET --insecure -b $COOKIE -s https://ip-cloudkey:port/api/s/default/stat/device/$MAC > $DEVICE
fi
if [ -f "$DEVICE" ]; then
cat $DEVICE | jq -r --argjson PORT "$1" '.data[].port_overrides[] | (select(.port_idx==$PORT) | (.poe_mode))'
fi
rm -f $DEVICE
rm -f $COOKIE
#!/bin/bash
if [ $# -lt 3 ]; then
echo "Usage : $0 port-nr state device"
exit
fi
if [ "$3" = "uap" ]; then
MAC=ma:co:fu:ap
else
MAC=ma:co:fu:sw
fi
DEVICE=/tmp/device-$3.json
COOKIE=/tmp/cookie.txt
PORTS=/tmp/ports-$3.json
ID=0
if [ ! -f "$COOKIE" ]; then
curl -X POST --insecure -H "Content-Type: application/json" -d '{
"username": "user",
"password": "password",
"remember": true
}' -c $COOKIE -o /dev/null -s https://ip-cloudkey:port/api/login
fi
if [ ! -f "$DEVICE" ]; then
curl -X GET --insecure -b $COOKIE -s https://ip-cloudkey:port/api/s/default/stat/device/$MAC > $DEVICE
fi
if [ -f "$DEVICE" ]; then
cat $DEVICE | jq "{port_overrides: .data[].port_overrides}" > $PORTS
ID=$(cat $DEVICE | jq -r '.data[]._id')
fi
if [ -f "$PORTS" ]; then
tmpfile=$(mktemp); cp $PORTS "$tmpfile" && jq --argjson PORT "$1" --arg MODE "$2" '.port_overrides[] |= (select(.port_idx==$PORT) |= (.poe_mode = $MODE))' "$tmpfile" > $PORTS && r
m -f "$tmpfile"
fi
sleep $((RANDOM % 10))
if [ -f "$DEVICE" ] && [ -f "$PORTS" ]; then
curl -X PUT --insecure -H "Content-Type: application/json" -b $COOKIE -d @$PORTS -o /dev/null -s https://ip-cloudkey:port/api/s/default/rest/device/$ID
fi
rm -f $DEVICE
rm -f $PORTS
rm -f $COOKIE
@jvandijk
Copy link
Author

jvandijk commented Jun 3, 2022

Explanation of this script is described on this blogpost : https://jrdk.nl/blog/2022/06/unifi-access-point-poe-switching-in-homekit/

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