Last active
February 4, 2016 01:26
-
-
Save bfg100k/8cf2118d6e01ff4cfee0 to your computer and use it in GitHub Desktop.
Ash script to manage the on/off state of the 5GHz band radio based on usage (i.e. devices connected) for Asus routers running custom firmware by Padavan (https://code.google.com/p/rt-n56u/)
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/bash | |
# Script to auto start/stop 5GHz wireless radio based on presence of | |
# certain MAC addresses on the LAN. | |
# CODITION TO : | |
# * TURN ON radio - one or more device(s) is/are on LAN | |
# * TURN OFF radio - ALL devices are _NOT_ on 5GHz band | |
# | |
# REQUIRED ENTWARE PACKAGES : | |
# * wget - Get files over HTTP/S, FTP | |
# | |
# Author: SidneyC <sidneyc_at_outlook_dot_com> | |
# | |
# CHANGELOG | |
# --------- | |
# 27/01/2015 Initial release | |
# 01/02/2015 Made path to wget explicit to avoid issues when running via cron | |
# Added sanity check to make sure we get a response back from wget | |
# before proceeding any further | |
# | |
################################################################################# | |
# path to file containing credentials to access the web UI | |
# file should contain only a single line in the format username,password | |
# with no spaces before and after. | |
# NOTE: THIS FILE MUST BE MANUALLY CREATED FIRST BEFORE RUNNING THIS SCRIPT | |
WEB_ACCESS_FILE="/opt/home/admin/.webaccess" | |
# List of MAC addresses to check for is in the file specified below. | |
# Note that this script will append new/unseen 5GHz devices to this | |
# file if they are connected to the 5GHz band at the time this script is run | |
MACS_FILE="/opt/etc/wlan5_trigger_macs" | |
# This script uses the wget package from entware as the default implementation | |
# in busybox does not handle HTTP authentication | |
WGET_BIN="/opt/bin/wget" | |
# INTERNAL VARIABLES | |
BASE_URL="http://localhost" | |
MACS_LIST="" | |
# SUBROUTINES | |
Get5gDevicesOn2g() { | |
WEB_OUTPUT=$($WGET_BIN -qO- --user=$WEB_USER --password=$WEB_PSWD $BASE_URL/Main_WStatus2g_Content.asp | sed -n "/<textarea .*>/,/<\/textarea>/p" | sed "s/<.*>//") | |
# SANITY CHECK - make sure we get something back in the output | |
if [[ -z "$WEB_OUTPUT" ]]; then | |
echo "[`date`] FATAL ERROR - did not get a valid response from web UI." | |
exit 1; | |
fi | |
#echo "DEBUG- $WEB_OUTPUT" | |
DEV_CONNECTED=$(echo "$WEB_OUTPUT" | grep -E "^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})" | cut -d ' ' -f1) | |
#echo "DEBUG- $DEV_CONNECTED" | |
G5DO2="" #initialise the var | |
for i in $DEV_CONNECTED; do | |
#echo "DEBUG - in loop - $i" | |
if [[ ! -z $(echo "$MACS_LIST" | grep "$i") ]]; then | |
if [[ -z $G5DO2 ]]; then | |
G5DO2="$i" | |
else | |
G5DO2="$G5DO2 $i" | |
fi | |
if [[ $1 == "one" ]]; then #user only asked for the first device found | |
#no more tests needed, let's break out of the loop | |
break | |
fi | |
fi | |
done | |
} | |
FindHostnameFromMac() { | |
FHFM=$(sed -n "s/.*$1,\(.*\),.,.,./\1/p" /tmp/static_ip.inf) | |
} | |
# Let's try to retreive the web access username & password | |
WEB_USER=$(cut -d ',' -f1 $WEB_ACCESS_FILE) | |
WEB_PSWD=$(cut -d ',' -f2 $WEB_ACCESS_FILE) | |
# SANITY CHECK #1 - make sure we have valid login credentials to access web UI | |
if [[ -z "$WEB_USER" || -z "$WEB_PSWD" ]]; then | |
echo "[`date`] ERROR - Could not retreive credentials to access web UI from file ($WEB_ACCESS_FILE). Does it exist or is in the correct format?" | |
exit 1; | |
fi | |
# SANITY CHECK #2 - if MACS_FILE does not exist, create it | |
if [[ ! -e $MACS_FILE ]]; then | |
echo "[`date`] WARNING - file ($MACS_FILE) does not exist. Creating one." | |
touch $MACS_FILE | |
fi | |
# Get the list of macs we will use to search for | |
MACS_LIST=$(grep -v '^#' $MACS_FILE | cut -d ',' -f1) | |
#echo "DEBUG- $MACS_LIST" | |
# we use the output of the Wireless 5GHz page for most of our checks | |
#wget -P=/tmp --user=$WEB_USER --password=$WEB_PSWD $BASE_URL/Main_WStatus_Content.asp | |
# zoom to the section we are after | |
#WEB_OUTPUT=$(sed -n "/<textarea .*>/,/<\/textarea>/p" /tmp/Main_WStatus2g_Content.asp) | |
WEB_OUTPUT=$($WGET_BIN -qO- --user=$WEB_USER --password=$WEB_PSWD $BASE_URL/Main_WStatus_Content.asp | sed -n "/<textarea .*>/,/<\/textarea>/p" | sed "s/<.*>//") | |
#echo "DEBUG- $WEB_OUTPUT" | |
# SANITY CHECK - make sure we get something back in the output | |
if [[ -z "$WEB_OUTPUT" ]]; then | |
echo "[`date`] FATAL ERROR - did not get a valid response from web UI." | |
exit 1; | |
fi | |
# start by checking status of 5GHz radio | |
if [[ -z "$(echo "$WEB_OUTPUT" | grep "Radio 5GHz is disabled")" ]]; then | |
echo "[`date`] 5GHz radio is on." | |
# 5GHz radio is on. let's see if any devices are connected | |
DEV_CONNECTED=$(echo "$WEB_OUTPUT" | grep -E "^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})" | cut -d ' ' -f1) | |
#echo "DEBUG- $DEV_CONNECTED" | |
if [[ ! -z "$DEV_CONNECTED" ]]; then | |
echo "[`date`] `echo "$DEV_CONNECTED" | wc -l` device(s) connected to 5GHz radio." | |
#yes, let's add any new devices we have not seen into the MACS_FILE | |
for i in $DEV_CONNECTED; do | |
#NOTE - for the test below, we use $MACS_FILE instead of $MACS_LIST | |
# because it may exist in file but commented out by user | |
if [[ -z "$(grep "$i" $MACS_FILE)" ]]; then | |
#found one, let's add it | |
#let's try to find and add the hostname if available | |
FindHostnameFromMac $i | |
echo "$i,$FHFM,added on `date`" >> $MACS_FILE | |
if [[ -z $FHFM ]]; then | |
FHFM=$i | |
fi | |
echo "[`date`] Found a new device ($FHFM) on 5GHz radio. Adding to $MACS_FILE." | |
fi | |
done | |
else | |
echo "[`date`] No devices connected to 5GHz radio." | |
#no devices are connected. Before we shut it down, check to see | |
#if any 5GHz device is connected to the 2.4GHz band. If so, do nothing. | |
Get5gDevicesOn2g "one" | |
if [[ -z $G5DO2 ]]; then | |
echo "[`date`] Did not find any 5GHz devices connected to 2.4GHz radio as well. Shutting down 5GHz radio." | |
/sbin/radio5_disable | |
else | |
FindHostnameFromMac $G5DO2 | |
if [[ -z $FHFM ]]; then | |
FHFM=$G5DO2 | |
fi | |
echo "[`date`] Found at least 1 5GHz device ($FHFM) connected to 2.4GHz radio. Leaving 5GHz radio on." | |
fi | |
fi | |
else | |
echo "[`date`] 5GHz radio is off." | |
# 5GHz radio is off. Let's scan the 2.4GHz list for any 5GHz devices that are connected to it | |
Get5gDevicesOn2g "one" | |
if [[ ! -z $G5DO2 ]]; then | |
#yes, we have one or more such devices. Let's turn on the 5GHz radio | |
FindHostnameFromMac $G5DO2 | |
if [[ -z $FHFM ]]; then | |
FHFM=$G5DO2 | |
fi | |
echo "[`date`] Found a 5GHz device ($FHFM) on 2.4GHz radio. Starting 5GHz radio." | |
/sbin/radio5_enable | |
else | |
echo "[`date`] Did not find any 5GHz devices connected to 2.4GHz radio. Leaving 5GHz radio off." | |
fi | |
fi | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment