Last active
February 14, 2023 07:16
-
-
Save ChenZhongPu/badbcfbffd1b8f31588278c935bdc023 to your computer and use it in GitHub Desktop.
Shell to list all connected devices of a hotspot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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