如何使用 iperf 输出并仅 grep 平均带宽?

如何使用 iperf 输出并仅 grep 平均带宽?

我在这里寻找一种使用脚本显示 iperf 带宽平均值的方法。我不确定命令有什么问题,我没有显示带宽的输出。我需要输出类似这样的内容

平均.......936.8 Mbits/秒

if [ "$#" -ne "2" ]; then
  echo "ERROR: script needs four arguments, where:"
  echo
  echo "1. Number of times to repeat test (e.g. 10)"
  echo "2. Host running 'iperf -s' (e.g. somehost)"
  echo
  echo "Example:"
  echo "  $(basename $0) 10 somehost"
  echo 
  echo "The above will run 'iperf -c' 10 times on the client and report totals and average."
  exit 1
else
  runs=$1
  host=$2
fi

log=iperf.$host.log

if [ -f $log ]; then
  echo removing $log
  rm $log
fi

echo "=================================================================="
echo " Results"
echo "=================================================================="
echo " target host .... $host"
echo "------------------------------------------------------------------"

for run in $(seq 1 $runs); do
  iperf -c $host -f m >> $log
  echo -e " run $run: \t $(awk '/Bandwidth/ {getline}; END{print $7, $8}' $log)"
done

avg=$(awk -v runs=$runs '/Bandwidth/ {getline; sum+=$7; avg=sum/runs} END {print avg}' $log)


echo "------------------------------------------------------------------"
echo " average ....... $avg Mbits/sec"

答案1

使用 json 输出?这实际上是为了便于解析,而不是为了便于阅读。

iperf3 支持 JSON 输出,可以使用例如jq或任何其他 JSON 解析器进行解析。

平均值将采用end.streamsjson 输出的结构:

    "end":  {
        "streams":  [{
                "sender":   {
                    "socket":   5,
                    "start":    0,
                    "end":  10.00009,
                    "seconds":  10.00009,
                    "bytes":    191102976,
                    "bits_per_second":  152881004.87095615,
                    "retransmits":  813,
                    "max_snd_cwnd": 1465304,
                    "max_snd_wnd":  3145728,
                    "max_rtt":  119453,
                    "min_rtt":  48135,
                    "mean_rtt": 60457,
                    "sender":   true
                },
                "receiver": {
                    "socket":   5,
                    "start":    0,
                    "end":  10.048346042633057,
                    "seconds":  10.00009,
                    "bytes":    188867176,
                    "bits_per_second":  150366776.93915045,
                    "sender":   true
                }

相关内容