Skip to content

Instantly share code, notes, and snippets.

@ChenZhongPu
Last active February 14, 2023 07:16
Show Gist options
  • Save ChenZhongPu/badbcfbffd1b8f31588278c935bdc023 to your computer and use it in GitHub Desktop.
Save ChenZhongPu/badbcfbffd1b8f31588278c935bdc023 to your computer and use it in GitHub Desktop.
Shell to list all connected devices of a hotspot
wifiInterface=$(iw dev | grep Interface | awk '{print $2}')
ssid=$(iw dev $wifiInterface info | grep ssid | awk '{print $2}')
echo "Your SSID is $ssid."
connections=$(ip neigh | grep $wifiInterface)
ips=()
macs=()
devices=()
while IFS= read -r conn;
do
lladdr=$(echo $conn | awk '{print $4}')
if [[ $lladdr == "lladdr" ]];
then
ip=$(echo $conn | awk '{print $1}')
mac=$(echo $conn | awk '{print $5}')
name=$(avahi-resolve --address $ip)
if [[ $name != "" ]];
then
device=$(echo $name | awk '{print $2}')
ips+=($ip)
macs+=($mac)
devices+=($device)
fi
fi
done <<< "$connections"
num=${#ips[@]}
plural() {
local dn="device"
if [[ num -gt 1 ]];
then
dn+="s"
fi
printf "%s" "$dn"
}
printf "$num $(plural) connecing to your hotspot!\n\n"
printf "%-15s %-25s %-20s\n" "IP" "MAC" "Device"
for (( i=0; i<${num}; i++ ));
do
printf "%-15s %-25s %-20s\n" "${ips[$i]}" "${macs[$i]}" "${devices[$i]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment