Skip to content

Instantly share code, notes, and snippets.

@joshbode
Last active March 2, 2016 01:00
Show Gist options
  • Save joshbode/6f7a8aec81f92e96bd6b to your computer and use it in GitHub Desktop.
Save joshbode/6f7a8aec81f92e96bd6b to your computer and use it in GitHub Desktop.
#! /usr/bin/env bash
GREP=ggrep
# parameters
BAND=$1
CHANNEL=${2:-0}
# determine the router ip
ROUTER=$(netstat -nr | $GREP -oP '(?<=^default) +[^ ]+' | $GREP -oP '[^ ]+')
# get username/password from .netrc
NETRC=${NETRC:-$HOME/.netrc}
USERNAME=$($GREP -zoP "(?<=^machine $ROUTER\n login ).+$" "$NETRC")
PASSWORD=$($GREP -zoP "(?<=^machine $ROUTER\n login $USERNAME\n password ).+$" "$NETRC")
# defaults for wlan
declare -A WLAN_DEFAULTS
WLAN_DEFAULTS=(
[WirelessEnable]=1
[OutputPower]=100
[NMode]=1
[restoreWirelessDefaults]=0
[commitwlanRadio]=1
)
# parsing regexes
declare -A CHECK
CHECK=(
[band]='(?<=Current&nbsp;:&nbsp; )\d(\.\d)? GHz'
[channel]='(?<=Current&nbsp;:&nbsp; )\d+(?= \*\*\*Interference Level: )'
[int]='(?<=Interference Level: )\w+'
)
# strip values from output
function check() {
curl -s http://$ROUTER/wlanRadio.asp | $GREP -aoP "${CHECK[$1]}"
}
# login
curl -d "loginUsername=$USERNAME&loginPassword=$PASSWORD" http://$ROUTER/goform/login
# update settings
if [[ $1 != "" ]]; then
echo -n "$(check band) ($(check channel)) -> "
QUERY="Band=$BAND&ChannelNumber=$CHANNEL"
QUERY=${QUERY}$(for x in "${!WLAN_DEFAULTS[@]}"; do echo -n "&$x=${WLAN_DEFAULTS[$x]}"; done)
curl -d "$QUERY" http://$ROUTER/goform/wlanRadio
fi
# check state
echo "$(check band) ($(check channel))"
echo "Interference: $(check int)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment