Skip to content

Instantly share code, notes, and snippets.

@ncwhale
Created April 11, 2020 10:16
Show Gist options
  • Save ncwhale/4cbcfaa8b86a9368df9d3eeebade4827 to your computer and use it in GitHub Desktop.
Save ncwhale/4cbcfaa8b86a9368df9d3eeebade4827 to your computer and use it in GitHub Desktop.
#!/bin/bash
MAC="1234567890AB"
COOKIE_FILE="/tmp/router_cookie.txt"
function fetch_result() {
local R=$(curl -s -b "$COOKIE_FILE" 'http://192.168.1.1/cgi-bin/luci/admin/device/devInfo?type=0&_=0.'"$(date +\"%s\")" | jq '.count = {}| .curip = {} | .[] | select(.mac)')
date
echo "$R" | jq --raw-output '"\(.mac)\t\(.ip)\t\(.restrict)\t\(.upSpeed)\t\(.downSpeed)"'
RESULT=$(echo "$R" | jq 'select(.mac == "'$MAC'")')
}
#LOGIN
curl -c "$COOKIE_FILE" 'http://192.168.1.1/cgi-bin/luci' -H 'Content-Type: application/x-www-form-urlencoded' --data 'username=useradmin&psd=gpuv4nZQXNPHJfq'
#GET IP
IP=$(curl -s -b "$COOKIE_FILE" 'http://192.168.1.1/cgi-bin/luci/admin/device/devInfo?type=0&_=0.'"$(date +\"%s\")" | jq '.count = {}| .curip = {} | .[] | select(.mac == "'$MAC'") | .ip')
echo "$IP"
#GET FORM TOKEN
TOKEN=$(curl -sN -b "$COOKIE_FILE" 'http://192.168.1.1/cgi-bin/luci/admin/device/pc?ip='"$IP" | grep -E -m 1 -o -e "\{ token: '(\w+)' \}" | grep -oP "(?<=').*(?=')")
echo "$TOKEN"
fetch_result
IS_BANNED=$(echo "$RESULT" | jq '.restrict')
if [ "$IS_BANNED" == "true" ] ; then
#RESET FIRST
curl -s -b "$COOKIE_FILE" 'http://192.168.1.1/cgi-bin/luci/admin/device/hostSettings' \
-H 'Content-type: application/x-www-form-urlencoded' \
--data 'token='"$TOKEN"'&type=restrict&onOff=0&mac='$MAC'&_=0.8532922919183523' | jq
sleep 2
fi
sleep 1
for _ in {0..30}; do
#GET RESULT
sleep 1
fetch_result
UPSPEED=$(echo "$RESULT" | jq '.upSpeed')
DOWNSPEED=$(echo "$RESULT" | jq '.downSpeed')
if [ "$UPSPEED" -gt 200000 ] || [ "$DOWNSPEED" -gt 2500000 ]; then
curl -s -b "$COOKIE_FILE" 'http://192.168.1.1/cgi-bin/luci/admin/device/hostSettings' \
-H 'Content-type: application/x-www-form-urlencoded' \
--data 'token='"$TOKEN"'&type=restrict&onOff=1&mac='$MAC'&_=0.8532922919183521'
# shellcheck disable=SC2034
for j in {0..30}; do
sleep 1
fetch_result
done
curl -s -b "$COOKIE_FILE" 'http://192.168.1.1/cgi-bin/luci/admin/device/hostSettings' \
-H 'Content-type: application/x-www-form-urlencoded' \
--data 'token='"$TOKEN"'&type=restrict&onOff=0&mac='$MAC'&_=0.8532922919183522'
break;
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment