핫스팟에 연결된 두 Wi-Fi 장치의 신호 강도를 인쇄하려고 합니다. 나는 이 스크립트를 실행하여 IP, 호스트 이름, Mac 및 신호를 인쇄했습니다. 아래 스크립트에서 Mac은 $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를 제거하고 모든 백틱을 $()
.