我正在尝试打印连接到热点的两个 wifi 设备的信号强度。我设法执行这个脚本来打印 ip、主机名、mac 和信号。在下面的脚本中,macs 存储在 中$maclist
,与信号相同$signallist
,问题是当我尝试执行循环时,它只打印存储的第一个信号,而不打印第二个信号。
#!/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
输出
# 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
编辑:我附上了“iw dev wlan0 station dump”输出
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
答案1
由于您使用的是 bash,因此您不妨利用数组而不是循环未加引号的变量。我不确定您从 signallist 提供了什么输入,但您的printf
命令位于该循环之外。这意味着它可能会循环多个信号,但每个 mac 地址只会提供一次输出。尝试将其移至信号列表循环中
#!/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
另外,我引用了您的变量,删除了 UUOC,并将所有反引号更改为$()
.