Logs

iperf3 結果在日誌文件中應該看起來不錯

  • August 16, 2018

我有這個結果:

Wed Aug 15 19:35:11 CEST 2018
Connecting to host x.x.x.x, port 5201
[  4] local x.x.x.x port 48944 connected to x.x.x.x port 5201
[ ID] Interval           Transfer     Bandwidth       Retr  Cwnd
[  4]   0.00-1.00   sec   375 MBytes  3.14 Gbits/sec  273    471 KBytes
[  4]   1.00-2.00   sec   428 MBytes  3.59 Gbits/sec  145    376 KBytes
[  4]   2.00-3.00   sec   360 MBytes  3.02 Gbits/sec  148    454 KBytes
[  4]   3.00-4.00   sec   339 MBytes  2.84 Gbits/sec   83    407 KBytes
[  4]   4.00-5.00   sec   305 MBytes  2.56 Gbits/sec  104    414 KBytes
[  4]   5.00-6.00   sec   301 MBytes  2.53 Gbits/sec  186    440 KBytes
[  4]   6.00-7.00   sec   325 MBytes  2.73 Gbits/sec  174    485 KBytes
[  4]   7.00-8.00   sec   434 MBytes  3.64 Gbits/sec   81    677 KBytes
[  4]   8.00-9.00   sec   412 MBytes  3.46 Gbits/sec  226    537 KBytes
[  4]   9.00-10.00  sec   409 MBytes  3.43 Gbits/sec   47    372 KBytes
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval           Transfer     Bandwidth       Retr
[  4]   0.00-10.00  sec  3.60 GBytes  3.09 Gbits/sec  1467             sender
[  4]   0.00-10.00  sec  3.60 GBytes  3.09 Gbits/sec                      receiver

我只想要這樣的一行:-)

17-08-15 19:35:11 0.00-10.00  sec  3.60 GBytes  3.09 Gbits/sec  1467

這怎麼可能?

我已經開始編寫這樣的腳本,但我認為我應該使用 awk、cut 等。

host=x.x.x.x
log=/data/div/sh/iperf.log
logr=reverseiperf.log
runs=2

for run in $(seq 1 $runs); do
   date >> $log && iperf3 -c $host >> $log
done

這不是最簡單的方法,但希望能向您展示“一種方法”:

$ printf "%s%s\n" \
  "$(TZ=CEST date -d "$(head -1 iperf.txt)" "+%y-%m-%d %H:%M:%S")" \
  "$(grep sender iperf.txt | awk -F"]  " '{print $2}')"
18-08-15 19:35:11 0.00-10.00  sec  3.60 GBytes  3.09 Gbits/sec  1467             sender

這工作如下:

  • printf "%s%s\n"- 列印我們將從您的iperf.txt輸出中解析的 2 個字元串
  • "$(TZ=CEST date -d "$(head -1 iperf.txt)" "+%y-%m-%d %H:%M:%S")"``iperf.txt- 從( )解析第一行head -1並將其傳遞給date命令,並以範例顯示的格式重新格式化日期
  • "$(grep sender iperf.txt | awk -F"] " '{print $2}')"- 解析包含字元串的行,sender然後在字元上分割這一行,列印被分割的參數]的右側。]這個結果是$2

引用自:https://unix.stackexchange.com/questions/462836