Skip to content

Instantly share code, notes, and snippets.

@lg
Last active Aug 16, 2022
Embed
What would you like to do?
auto disconnects clients with a low signal strength on LEDE / OpenWRT
#!/bin/ash
#
# angry_wifi.sh
#
# auto disconnects clients with a low signal strength on LEDE / OpenWRT. great
# for clients who hold onto a lower-signal-strength accesspoint instead of
# roaming. before running, make sure to download the latest MAC addresses with:
#
# wget --no-check-certificate -O - 'https://services13.ieee.org/RST/standards-ra-web/rest/assignments/download/?registry=MAC&text=apple' | cut -d, -f2 > apple_mac_addresses
#
# some notes:
# - only Apple clients will be disconnected
# - clients won't be re-disconnected for 1 minute if they end up re-connecting
# - be careful with this script, if you don't have solid wifi everywhere clients
# will constantly be disconnected
IFS=$'\n'
while true; do
for LINE in $(iwinfo wlan0 assoclist | grep SNR); do
MAC=$(echo "$LINE" | awk '{ print $1 }')
SIGNAL=$(echo "$LINE" | awk '{ print $2 }')
MAC_PREFIX=$(echo "$MAC" | sed -e 's/://g' | cut -c1-6)
if [ "$SIGNAL" -lt "-80" ]; then
if find . | grep "angry_wifi_client_$MAC" > /dev/null; then
date ; logger -s -t angry_wifi "Low signal client $MAC ($SIGNAL) is back, not disconnecting yet."
# Only allow Apple devices to roam
elif grep "$MAC_PREFIX" apple_mac_addresses; then
date ; logger -s -t angry_wifi "Low signal client $MAC ($SIGNAL) being disconnected."
ubus call hostapd.wlan0 del_client "{'addr':'$MAC', 'reason':5, 'deauth':false, 'ban_time':0}"
# Add to do-not-disconnect list
touch "angry_wifi_client_${MAC}_$(date +%s)"
fi
fi
done
# Remove from the do-not-disconnect list
CURDATE=$(date +%s)
for FILE in $(find . | grep angry_wifi_client); do
TIME=$(echo "$FILE" | cut -d'_' -f 5)
TIME_SINCE=$((CURDATE - TIME))
MAC=$(echo "$FILE" | cut -d'_' -f 4)
if [ "$TIME_SINCE" -gt "60" ]; then
date ; logger -s -t angry_wifi "Low signal client $MAC removed from do-not-disconnect."
rm "$FILE"
fi
done
sleep 1
done
@lifeeth
Copy link

lifeeth commented Oct 22, 2016

Thanks for sharing this. Was looking to hack up something similar and google pointed me to you :)

@benjamin-army
Copy link

benjamin-army commented Nov 22, 2016

only Apple clients will be disconnected

Why is that?

Thanks

@barbieri
Copy link

barbieri commented Feb 14, 2018

I needed something more robust, so I got concepts from this gist and https://github.com/mk248/lede-modhostapd and created a pure-Lua script that uses /etc/config/wireless, taking signal OR signal-noise-ratio, supports whitelists (like Apple devices), blacklists and is a bit more efficient, not touching the filesystem to persist state.

https://github.com/barbieri/barbieri-playground/tree/master/openwrt/wifi-disconnect-low-signal

however I really hope OpenWRT/LEDE will integrate the modhostapd patch, then we can avoid an extra process in the system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment