Skip to content

Instantly share code, notes, and snippets.

@pocki80
Last active May 22, 2020 10:48
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 pocki80/6960085702072220ed48e1137dd5e080 to your computer and use it in GitHub Desktop.
Save pocki80/6960085702072220ed48e1137dd5e080 to your computer and use it in GitHub Desktop.
Place "powercycle" shortcut to easily reboot an attached antenna
#!/bin/bash
# persistently locate the powercycle script
#
# INSTALL/UPDATE via CLI:
# curl -skL https://gist.github.com/pocki80/6960085702072220ed48e1137dd5e080/raw/install_powercycle_script.sh | sudo bash
cat >/config/scripts/post-config.d/powercyclescriptsetup.sh <<'ENDINSTALLCONTENT'
#!/bin/bash
# install powercycle script file if missing
cat >/tmp/powerscriptfile <<'ENDSCRIPTCONTENT'
#!/bin/bash
# Reboot an attached device
#
# /usr/local/bin/powercycle $port
all=$(/usr/sbin/ubnt-ifctl show-poe-status)
echo "$all"
for int in $(echo "$all" | awk '$1 ~ /^eth/ && $2 !~ /OFF/ { print $1}'); do
echo "$int speed "$(/usr/sbin/ubnt-hal getEthPhysical $int | sed -n 's/ *Speed: \(.*$\)/\1/p')
done
int="$1"
! [ "$int" ] && echo "no interface given" && exit 1
state=$(/usr/sbin/ubnt-ifctl show-poe-status | grep -E "^$int" | awk '{print $2}')
! [ "$state" ] && echo "no state recognized" && exit 2
[ "$state" == "OFF" ] && echo "currently OFF, will not change that" && exit 3
value=$(/usr/sbin/ubnt-ifctl show-poe-status | grep -E "^$int" | awk '{print $3}' | tr '[:upper:]' '[:lower:]')
! [ "$value" ] && echo "no voltage recognized" && exit 4
echo "Powering Down $int..."
/usr/sbin/ubnt-ifctl set-poe $int off
echo "Waiting for 2sec..."
sleep 2
echo "Powering Up $int using $state..."
/usr/sbin/ubnt-ifctl set-poe $int $value
echo "Done."
exit 0
ENDSCRIPTCONTENT
if [ ! -f /usr/local/bin/powercycle ]; then
#echo "initial install"
cp /tmp/powerscriptfile /usr/local/bin/powercycle
elif [ $(sha1sum /tmp/powerscriptfile | awk {'print $1'}) != $(sha1sum /usr/local/bin/powercycle | awk {'print $1'}) ]; then
#echo "updated file"
cp /tmp/powerscriptfile /usr/local/bin/powercycle
#else
#echo "nothing to do"
fi
if [ -f /usr/local/bin/powercycle ] && [ ! -x /usr/local/bin/powercycle ]; then
echo "chmod 755 file"
chmod 755 /usr/local/bin/powercycle
fi
rm /tmp/powerscriptfile
exit 0
ENDINSTALLCONTENT
chmod 755 /config/scripts/post-config.d/powercyclescriptsetup.sh
/config/scripts/post-config.d/powercyclescriptsetup.sh
echo "done"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment