0

I am here finding a way to display the average of iperf bandwidth using the script. I am not sure whats wrong with command, i dont get the output of Bandwidth displayed. I need output something like this

average ....... 936.8 Mbits/sec

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"
ls440
  • 1

1 Answers1

0

Use the json output? That's actually made for being parseable, not human readable.

iperf3 supports JSON output, which could potentially be parsed using e.g. jq or any other JSON parser.

The average would be in the end.streams structure of the json output:

    "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
                }
vidarlo
  • 3,775
  • 1
  • 12
  • 25
  • Should I parse like this ? avg=$(awk -v runs=$runs '/Bandwidth/ {getline; sum+=$7; avg=sum/runs} END {print avg}' $log) | jq – ls440 Sep 11 '22 at 10:26
  • I would argue that you should use `jq` or some other json aware parser to parse JSON. Not awk. – vidarlo Sep 11 '22 at 13:15
  • But bash does not do decimal calculations so the way to do with awk. ? – ls440 Sep 11 '22 at 15:06