Skip to content

Instantly share code, notes, and snippets.

@milankragujevic
Forked from lg/angry_wifi.sh
Last active May 8, 2021 10:53
Show Gist options
  • Save milankragujevic/d4952664c41d9358b9c0fd2d104903cd to your computer and use it in GitHub Desktop.
Save milankragujevic/d4952664c41d9358b9c0fd2d104903cd to your computer and use it in GitHub Desktop.
Auto disconnect WiFi clients with a low Received Signal Power on OpenWRT 19.07
rm /root/roaming_assistant.sh; wget http://10.25.100.10/roaming_assistant.sh -O /root/roaming_assistant.sh; chmod +x /root/roaming_assistant.sh
rm /root/monitor-ra.sh; wget http://10.25.100.10/monitor-ra.sh -O /root/monitor-ra.sh; chmod +x /root/monitor-ra.sh
rm /root/restart-ra.sh; wget http://10.25.100.10/restart-ra.sh -O /root/restart-ra.sh; chmod +x /root/restart-ra.sh
/root/restart-ra.sh
* * * * * bash /root/monitor-ra.sh >/dev/null 2>&1
#!/bin/ash
if [ -e "/root/DISABLE_RA" ]; then
exit 0
fi
if [ $(ps | grep -v grep | grep roaming_assistant.sh | wc -l) = 0 ]; then
/root/roaming_assistant.sh >/dev/null 2>&1 &
fi
#!/bin/ash
if [ -e "/root/DISABLE_RA" ]; then
exit 0
fi
kill -9 $(ps | grep -v grep | grep roaming_assistant.sh | awk '{print $1}')
sleep 1
/root/monitor-ra.sh &
echo "Restarted. "
#!/bin/ash
#
# roaming_assistant.sh
#
# Auto disconnect WiFi clients with a low Received Signal Power on OpenWRT 19.07
#
# Will disconnect all WiFi clients with RX signal < -75 dBm. Clients which reconnect
# back to the AP will be allowed to stay connected for 60s ("do-not-disconnect" list)
# Be careful with this script, as if you don't have great WiFi coverage everywhere
# clients will be constantly disconnected and keep reconnecting which will cause a
# high network load and provide poor quality of experience for the users.
#
# Suggestion: Best used with 802.11r for fast roaming to other APs
#
# ------------------------------------------------------------------------------------
# https://gist.github.com/milankragujevic/d4952664c41d9358b9c0fd2d104903cd
# ------------------------------------------------------------------------------------
#
# Based upon the work by Larry Gadea (https://gist.github.com/lg/e91d1c5c9640d963e13dbb1901bb4396)
#
# Copyright 2016, 2021 Larry Gadea, Milan Kragujevic
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
# associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute,
# sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or
# substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
# NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
if [ $(ps | grep -v grep | grep roaming_assistant.sh | wc -l) = 1 ]; then
exit 0
fi
if [ -e "/root/DISABLE_RA" ]; then
exit 0
fi
CFG_SIGNAL_THRESHOLD="-75" # set minimum received signal power in dBm
CFG_DO_NOT_DISCONNECT_TIMEOUT="60" # time to keep clients on the do-not-disconnect list, in seconds
rm -r /tmp/roaming
mkdir -p /tmp/roaming
while true; do
for wi in 0 1; do
DEVICE="wlan$wi"
iwinfo $DEVICE assoclist | grep SNR | while read -r LINE; do
MAC=$(echo "$LINE" | awk '{ print $1 }')
VENDOR=$(echo "$MAC" | cut -c1-8)
SIGNAL=$(echo "$LINE" | awk '{ print $2 }')
if [ "$SIGNAL" -lt "$CFG_SIGNAL_THRESHOLD" ]; then
if [ "$VENDOR" = "E0:09:BF" ] || [ "$VENDOR" = "1C:3B:F3" ]; then
logger -t roaming_assistant "$(date "+%Y-%m-%d %H:%M:%S") Not disconnecting client $MAC ($SIGNAL dBm) since vendor is whitelisted."
continue
fi
if find /tmp/roaming/ | grep "roaming_device_client_$MAC" > /dev/null; then
logger -t roaming_assistant "$(date "+%Y-%m-%d %H:%M:%S") Low signal client $MAC ($SIGNAL dBm) is back, not disconnecting yet."
else
logger -t roaming_assistant "$(date "+%Y-%m-%d %H:%M:%S") Low signal client $MAC ($SIGNAL dBm) being disconnected."
ubus call "hostapd.$DEVICE" del_client "{'addr':'$MAC', 'reason':5, 'deauth':false, 'ban_time':0}"
touch "/tmp/roaming/roaming_device_client_${MAC}_$(date +%s)"
fi
fi
done
done
CURDATE=$(date +%s)
for FILE in $(find /tmp/roaming/ | grep roaming_device); do
TIME=$(echo "$FILE" | cut -d'_' -f 5)
TIME_SINCE=$((CURDATE - TIME))
MAC=$(echo "$FILE" | cut -d'_' -f 4)
if [ "$TIME_SINCE" -gt "$CFG_DO_NOT_DISCONNECT_TIMEOUT" ]; then
logger -t roaming_assistant "$(date "+%Y-%m-%d %H:%M:%S") Low signal client $MAC removed from do-not-disconnect."
rm "$FILE"
fi
done
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment