From fdf136e4640e1ce281c3d416bc642e9a59b8c875 Mon Sep 17 00:00:00 2001 From: hugmouse Date: Wed, 19 Jun 2024 18:59:04 +0200 Subject: [PATCH] refactor: add check for 'shuf' inside 'check_command' function --- yabs.sh | 100 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 52 insertions(+), 48 deletions(-) diff --git a/yabs.sh b/yabs.sh index e14df9a..8044402 100644 --- a/yabs.sh +++ b/yabs.sh @@ -786,56 +786,60 @@ function iperf_test { # attempt the iperf send test 3 times, allowing for a slot to become available on the # server or to throw out any bad/error results - I=1 - while [ $I -le 3 ] - do - echo -en "Performing $MODE iperf3 send test to $HOST (Attempt #$I of 3)..." - # select a random iperf port from the range provided - PORT=$(shuf -i "$PORTS" -n 1) - # run the iperf test sending data from the host to the iperf server; includes - # a timeout of 15s in case the iperf server is not responding; uses 8 parallel - # threads for the network test - IPERF_RUN_SEND="$(timeout 15 "$IPERF_CMD" "$FLAGS" -c "$URL" -p "$PORT" -P 8 2> /dev/null)" - # check if iperf exited cleanly and did not return an error - if [[ "$IPERF_RUN_SEND" == *"receiver"* && "$IPERF_RUN_SEND" != *"error"* ]]; then - # test did not result in an error, parse speed result - SPEED=$(echo "${IPERF_RUN_SEND}" | grep SUM | grep receiver | awk '{ print $6 }') - # if speed result is blank or bad (0.00), rerun, otherwise set counter to exit loop - [[ -z $SPEED || "$SPEED" == "0.00" ]] && I=$(( I + 1 )) || I=11 - else - # if iperf server is not responding, set counter to exit, otherwise increment, sleep, and rerun - [[ "$IPERF_RUN_SEND" == *"unable to connect"* ]] && I=11 || I=$(( I + 1 )) && sleep 2 - fi - echo -en "\r\033[0K" - done + if check_command "shuf"; then + I=1 + while [ $I -le 3 ] + do + echo -en "Performing $MODE iperf3 send test to $HOST (Attempt #$I of 3)..." + # select a random iperf port from the range provided + PORT=$(shuf -i "$PORTS" -n 1) + # run the iperf test sending data from the host to the iperf server; includes + # a timeout of 15s in case the iperf server is not responding; uses 8 parallel + # threads for the network test + IPERF_RUN_SEND="$(timeout 15 "$IPERF_CMD" "$FLAGS" -c "$URL" -p "$PORT" -P 8 2> /dev/null)" + # check if iperf exited cleanly and did not return an error + if [[ "$IPERF_RUN_SEND" == *"receiver"* && "$IPERF_RUN_SEND" != *"error"* ]]; then + # test did not result in an error, parse speed result + SPEED=$(echo "${IPERF_RUN_SEND}" | grep SUM | grep receiver | awk '{ print $6 }') + # if speed result is blank or bad (0.00), rerun, otherwise set counter to exit loop + [[ -z $SPEED || "$SPEED" == "0.00" ]] && I=$(( I + 1 )) || I=11 + else + # if iperf server is not responding, set counter to exit, otherwise increment, sleep, and rerun + [[ "$IPERF_RUN_SEND" == *"unable to connect"* ]] && I=11 || I=$(( I + 1 )) && sleep 2 + fi + echo -en "\r\033[0K" + done - # small sleep necessary to give iperf server a breather to get ready for a new test - sleep 1 + # small sleep necessary to give iperf server a breather to get ready for a new test + sleep 1 - # attempt the iperf receive test 3 times, allowing for a slot to become available on - # the server or to throw out any bad/error results - J=1 - while [ $J -le 3 ] - do - echo -n "Performing $MODE iperf3 recv test from $HOST (Attempt #$J of 3)..." - # select a random iperf port from the range provided - PORT=$(shuf -i "$PORTS" -n 1) - # run the iperf test receiving data from the iperf server to the host; includes - # a timeout of 15s in case the iperf server is not responding; uses 8 parallel - # threads for the network test - IPERF_RUN_RECV="$(timeout 15 "$IPERF_CMD" "$FLAGS" -c "$URL" -p "$PORT" -P 8 -R 2> /dev/null)" - # check if iperf exited cleanly and did not return an error - if [[ "$IPERF_RUN_RECV" == *"receiver"* && "$IPERF_RUN_RECV" != *"error"* ]]; then - # test did not result in an error, parse speed result - SPEED=$(echo "${IPERF_RUN_RECV}" | grep SUM | grep receiver | awk '{ print $6 }') - # if speed result is blank or bad (0.00), rerun, otherwise set counter to exit loop - [[ -z $SPEED || "$SPEED" == "0.00" ]] && J=$(( J + 1 )) || J=11 - else - # if iperf server is not responding, set counter to exit, otherwise increment, sleep, and rerun - [[ "$IPERF_RUN_RECV" == *"unable to connect"* ]] && J=11 || J=$(( J + 1 )) && sleep 2 - fi - echo -en "\r\033[0K" - done + # attempt the iperf receive test 3 times, allowing for a slot to become available on + # the server or to throw out any bad/error results + J=1 + while [ $J -le 3 ] + do + echo -n "Performing $MODE iperf3 recv test from $HOST (Attempt #$J of 3)..." + # select a random iperf port from the range provided + PORT=$(shuf -i "$PORTS" -n 1) + # run the iperf test receiving data from the iperf server to the host; includes + # a timeout of 15s in case the iperf server is not responding; uses 8 parallel + # threads for the network test + IPERF_RUN_RECV="$(timeout 15 "$IPERF_CMD" "$FLAGS" -c "$URL" -p "$PORT" -P 8 -R 2> /dev/null)" + # check if iperf exited cleanly and did not return an error + if [[ "$IPERF_RUN_RECV" == *"receiver"* && "$IPERF_RUN_RECV" != *"error"* ]]; then + # test did not result in an error, parse speed result + SPEED=$(echo "${IPERF_RUN_RECV}" | grep SUM | grep receiver | awk '{ print $6 }') + # if speed result is blank or bad (0.00), rerun, otherwise set counter to exit loop + [[ -z $SPEED || "$SPEED" == "0.00" ]] && J=$(( J + 1 )) || J=11 + else + # if iperf server is not responding, set counter to exit, otherwise increment, sleep, and rerun + [[ "$IPERF_RUN_RECV" == *"unable to connect"* ]] && J=11 || J=$(( J + 1 )) && sleep 2 + fi + echo -en "\r\033[0K" + done + else + echo -e "\nWarning: 'shuf' command not found. Skipping iperf network tests..." + fi # Run a latency test via ping -c1 command -> will return "xx.x ms" [[ -n $LOCAL_PING ]] && LATENCY_RUN="$(ping -c1 "$URL" 2>/dev/null | grep -o 'time=.*' | sed s/'time='//)"