このフィールドをどのように追加しますか?

このフィールドをどのように追加しますか?

ホットスポットに接続されている2つのWi-Fiデバイスの信号強度を印刷しようとしています。私はこのスクリプトを実行してIP、ホスト名、Mac、およびシグナルを印刷しました。以下のスクリプトでは、Macは$maclist信号と同じように保存されます$signallist。問題は、ループを実行しようとしたときに保存された最初の信号だけを印刷し、2番目の信号は印刷しないことです。

#!/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を削除し、すべてのバックティックを$()

おすすめ記事