1

I'm trying to print the signal strength of two wifi devices connected to a hotspot. I manage to do this script that prints the ip, hostname, mac and signal. In the script below the macs are stored in $maclist, same with signals in $signallist, the problem is that when I try to do the loop it only prints the first signal stored but not the second.

#!/bin/bash

# show_wifi_clients.sh
# Shows MAC, IP address and any hostname info for all connected wifi devices

echo    "# All connected wifi devices, with IP address,"
echo    "# hostname (if available), and MAC address."
printf  "# %-20s %-30s %-20s %-20s\n" "IP address" "Lease name" "MAC address" "Signal"

leasefile=/var/lib/misc/dnsmasq.leases

# list all wireless network interfaces
# Gets "wlan0" for the variable interface

for interface in `iw dev | grep Interface | cut -f 2 -s -d" "`
do
  # for each interface, get mac addresses of connected stations/clients
  maclist=`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "`
  # for each interface, get their signals
  signallist=`iw dev $interface station dump | grep signal: | awk '{print $2}'`

  # for each mac address in that list...
  for mac in $maclist
  do
    # If a DHCP lease has been given out by dnsmasq,
    # save it.
    for signal in $signallist
    do
      ip="UNKN"
      host=""
      ip=`cat $leasefile | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 2 -s -d" "`
      host=`cat $leasefile | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 3 -s -d" "`
      # ... show the mac address:

     done
     printf "  %-20s %-30s %-20s %-20s\n" $ip $host $mac $signal
  done
done

Output

# All connected wifi devices, with IP address,
# hostname (if available), and MAC address.
# IP address        Lease name        MAC address     Signal              
  10.42...          device1           b8:27:eb:...    -45                 
  10.42...          device2           b4:9d:0b:...    -45

EDIT: I attach the "iw dev wlan0 station dump" output

Station b8:27:eb:... (on wlan0)
    inactive time:  39608 ms
    rx bytes:   141100
    rx packets: 3074
    tx bytes:   38351
    tx packets: 247
    tx retries: 522
    tx failed:  81
    rx drop misc:   0
    signal:     -20 [-20, -39] dBm
    signal avg: -24 [-24, -49] dBm
    tx bitrate: 6.5 MBit/s MCS 0
    rx bitrate: 7.2 MBit/s MCS 0 short GI
    expected throughput:    4.394Mbps
    authorized: yes
    authenticated:  yes
    associated: yes
    preamble:   short
    WMM/WME:    yes
    MFP:        no
    TDLS peer:  no
    DTIM period:    2
    beacon interval:100
    short slot time:yes
    connected time: 1990 seconds
Station b4:9d:0b:... (on wlan0)
    inactive time:  22480 ms
    rx bytes:   3559209
    rx packets: 28452
    tx bytes:   61838932
    tx packets: 55337
    tx retries: 1375
    tx failed:  152
    rx drop misc:   9
    signal:     -45 [-52, -46] dBm
    signal avg: -43 [-49, -46] dBm
    tx bitrate: 72.2 MBit/s MCS 7 short GI
    rx bitrate: 6.0 MBit/s
    expected throughput:    33.507Mbps
    authorized: yes
    authenticated:  yes
    associated: yes
    preamble:   long
    WMM/WME:    yes
    MFP:        no
    TDLS peer:  no
    DTIM period:    2
    beacon interval:100
    short slot time:yes
    connected time: 1948 seconds
  • What is the output of just iw dev "$interface" station dump? – Jesse_b Apr 18 at 0:05
  • "$interface" is wlan0 and it will show information of the wifi device like its mac address, signal strength and more that is not relevant to what I want to achieve. – Alvaromr7 Apr 18 at 20:59
  • Please provide me the exact output of iw dev "$interface" station dump – Jesse_b Apr 18 at 21:13
  • I've edited the post with what you ask. – Alvaromr7 Apr 19 at 14:42
  • I’m confused.  Your script shows that it gets the value of signal from signallist, which gets its value(s) from the output of iw dev $interface station dump.  And your sample output shows signal values of -45.  But -45 does not appear anywhere in the output of iw dev wlan0 station dump that you show, and 45 appears only in the context of connected time: 9145 seconds, and there’s no suggestion that your script is looking at that.  Further, you mention -35 and 60 in a comment, and they don’t appear in the station dump, either.  … (Cont’d) – G-Man Says 'Reinstate Monica' Apr 19 at 16:08
0

Since you are using bash you might as well take advantage of arrays instead of looping over unquotted variables. I'm not sure what input you are providing from signallist though, but your printf command is outside that loop. That means it may be looping through multiple signals but will only provide output once per mac address. Try moving it into the signallist loop

#!/bin/bash

echo    "# All connected wifi devices, with IP address,"
echo    "# hostname (if available), and MAC address."
printf  "# %-20s %-30s %-20s %-20s\n" "IP address" "Lease name" "MAC address" "Signal"

leasefile=/var/lib/misc/dnsmasq.leases

for interface in $(iw dev | grep Interface | cut -f 2 -s -d" "); do
  maclist=( $(iw dev "$interface" station dump | grep Station | cut -f 2 -s -d" ") )
  signallist=( $(iw dev "$interface" station dump | awk '/signal:/{print $2}') )

  for mac in "${maclist[@]}"; do
    for signal in "${signallist[@]}"; do
      ip="UNKN"
      host=""
      ip=$(cut -f 2,3,4 -s -d" " "$leasefile" | grep $mac | cut -f 2 -s -d" ")
      host=$(cut -f 2,3,4 -s -d" " "$leasefile" | grep $mac | cut -f 3 -s -d" ")
      printf "  %-20s %-30s %-20s %-20s\n" "$ip" "$host" "$mac" "$signal"
     done
  done
done

Additionally I have quoted your variables, removed the UUOCs, and changed all backticks to $().

  • I tried what you told me but it doesn't work either. Using the solution you gave me I got the same output repeated twice and the signallist contains two numbers say, -35 and 60. – Alvaromr7 Apr 17 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.