Disconnect Client from any WiFi on any Router - Script

I was looking for a way to automatically disconnect a host from my wifi, and found this thread: Disconnect Client from WiFi using CLI

I've packed that command into a script that loops over all networks so I don't need to know which network (or vlan) the target mac is connected to:

#!/bin/sh

echo "Welcome to the OpenWRT host disconnector script!"

if [ -n "$1" ]; then
  target="$1"
else
  echo "please specify target host you want to disconnect!"
  exit 1
fi
echo "target-mac: $target"

hex="[0-9a-fA-F]"
if [[ "$target" =~ ^$hex{2}:$hex{2}:$hex{2}:$hex{2}:$hex{2}:$hex{2}$ ]]; then
  echo "target mac has correct format"
else
  echo "target mac doesn't match the correct mac adress format!"
  exit 2
fi

while IFS= read -r network; do
  #echo "network: $network"
  info=$(iwinfo $network assoclist | grep -A 3 "$target")
  if [ -n "$info" ]; then
    echo "host connected to network $network:"
    echo "$info"
    network_no_vlan=$(echo "$network" | cut -d "-" -f1,2)
    network_ubus="hostapd.$network_no_vlan"
    ubus call $network_ubus del_client "{'addr':'$target', 'reason':5, 'deauth':true, 'ban_time':1000}"
    echo "have disconnected it, other hosts still connected to $network are:"
    # opkg install coreutils-sleep # needed for sleep with fractional values
    sleep 0.1 # give it 100ms time to disconnect..
    iwinfo "$network" assoclist
    exit 0
  else
    echo "host $target not connected to network $network"
  fi
done < <(iwinfo | grep -A 1 '^$' | grep -v '\-\-' | grep -v '^$' | cut -d ' ' -f1)

echo "The specified host was not connected to any of this routers wifi networks, so we couldn't disconnect it. Sorry."
exit 3

And on the central linux box so that it doesn't have to know which OpenWRT access point the client is currently connected to I've got this script that tries the second one if the script on the first one doesn't return exit code 0:

target="$1"
ssh root@openwrt1 /root/disconnect_host.sh "$target" || ssh root@openwrt2 /root/disconnect_host.sh "$target"

I would have put this in the thread I've found the command on, but sadly it

was automatically closed 10 days after the last reply. New replies are no longer allowed.

This is not really a question, I just wanted to publish this here :wink:

3 Likes