Skip to content

Instantly share code, notes, and snippets.

@oxavelar
Last active March 1, 2026 13:41
Show Gist options
  • Select an option

  • Save oxavelar/5be72dc2b08f7743c0b46c22f4f7a01f to your computer and use it in GitHub Desktop.

Select an option

Save oxavelar/5be72dc2b08f7743c0b46c22f4f7a01f to your computer and use it in GitHub Desktop.
(
disconnect() {
local dev="$1" mac="$2"
echo "$dev: disconnect $mac" > /dev/kmsg
ubus call "hostapd.$dev" del_client "{'addr':'$mac','reason':8,'deauth':true,'ban_time':0}"
}
checkalive() {
local dev="$1" mac="$2" inactive
inactive="$(iw dev "$dev" station get "$mac" 2>/dev/null | sed -n 's/.*inactive time:[[:space:]]*\([0-9]*\).*/\1/p')"
[ -z "$inactive" ] || [ "$inactive" -gt 60000 ] && disconnect "$dev" "$mac"
}
mt79_stuck_monitor() {
local dev="$1"
echo "$dev: watchdog [mt79-stuck-monitor]" > /dev/kmsg
(
while true; do
sleep 10
json="$(ubus call "hostapd.$dev" get_clients 2>/dev/null)" || continue
while read -r mac; do
[ -z "$mac" ] && continue
tx="$(echo "$json" | jsonfilter -e "@.clients[\"$mac\"].airtime.tx")"
rx="$(echo "$json" | jsonfilter -e "@.clients[\"$mac\"].airtime.rx")"
[ -z "$tx" ] || [ -z "$rx" ] && continue
txvar="tx_${mac//:/_}"
rxvar="rx_${mac//:/_}"
eval prev_tx=\${$txvar:-$tx}
eval prev_rx=\${$rxvar:-$rx}
eval $txvar=$tx
eval $rxvar=$rx
dtx=$((tx - prev_tx))
drx=$((rx - prev_rx))
if [ "$dtx" -eq 0 ] && [ "$drx" -eq 0 ]; then
checkalive "$dev" "$mac"
fi
done <<EOF
$(echo "$json" | jsonfilter -e '@.clients' 2>/dev/null | grep -oE '([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}')
EOF
done
) &
}
# Ensure necessary services are up
sleep 40
# Start bogus-client monitoring for APs
for dev in $(iwinfo | awk '/phy[0-9]-ap[0-9]/ { print $1 }'); do
mt79_stuck_monitor "$dev" &
done
)
@oxavelar
Copy link
Author

oxavelar commented Dec 8, 2024

If this is working you should see something like this:

[   54.071222] phy0-ap0: watchdog [mt79-stuck-monitor]
[   54.071519] phy1-ap0: watchdog [mt79-stuck-monitor]
[   54.071526] phy0-ap1: watchdog [mt79-stuck-monitor]
[   54.071886] phy1-ap1: watchdog [mt79-stuck-monitor]

With events following:

[  740.326456] phy0-ap1: disconnect c0:84:ff:32:d2:a4
[ 1466.660469] phy0-ap1: disconnect c0:84:ff:30:bb:fc

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