This is a nice script for iperf tests:
#!/bin/bash # A menu driven shell script sample template ## ---------------------------------- # Step #1: Define variables # ---------------------------------- RED='\033[0;41;30m' STD='\033[0;0;39m'
#Get current interface: curint=`ip addr | grep 'state UP' | awk -F: '{print $2}' ` if [ -z "${curint}" ]; then echo "The interface is down, please make sure you are connected to the network" exit 0 fi if [ -z "${curint+set}" ]; then echo "The interface is down, please make sure you are connected to the network" exit 0 fi if [ -z "${curint-unset}" ]; then echo "The interface is down, please make sure you are connected to the network" exit 0 fi if [ -n "${curint}" ]; then echo "The interface ${curint} is UP" fi
#Get current ip address: # curadres=`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'` curadres=`ip addr | grep 'state UP' -A2 | grep "inet " | awk '{print $2}'` if [ -z "${curadres}" ]; then echo "The ip address is not set" read -p "Enter your ip address:" myaddress ifconfig ${curint} ${myaddress}/24 fi if [ -n "${curadres}" ]; then echo "The ip address is set and is: ${curadres}" fi
#Give remote ip address: read -p "Enter remote ip address:" address address2="${address}" #Give tiem to test is seconds: read -p "How long do you want to test (in seconds)?:" time time2="${time}"
# ---------------------------------- # Step #2: User defined function # ---------------------------------- pause(){ read -p "Press [Enter] key to continue..." fackEnterKey } one(){ echo "Set iperf Server TCP" now=$(date +"%F_%H%M%S") LOG_FILE="/root/iperf-data/$now-tcp-server.txt" /bin/iperf -s -p 5001 > >(tee -a ${LOG_FILE} ) } two(){ echo "Set iperf Client 8K" now=$(date +"%F_%H%M%S") read -e -i "$address2" -p "Enter remote ip address:: " address3 address2="${address3:-$address2}" read -e -i "${time2}" -p "How long do you want to test (in seconds)?:" time3 time2="${time3:-$time2}" echo "Remote Address: $address3 Time: $time3" /bin/iperf -c $address3 -p 5001 -t $time3 -i 1 -w 8K | tee "/root/iperf-data/$now-tcp-8K-client.txt" } three(){ echo "Set iperf Client 64K" now=$(date +"%F_%H%M%S") echo "Remote Address: $address Time: $time" read -e -i "$address2" -p "Enter remote ip address:: " address3 address2="${address3:-$address2}" read -e -i "${time2}" -p "How long do you want to test (in seconds)?:" time3 time2="${time3:-$time2}" echo "Remote Address: $address3 Time: $time3" /bin/iperf -c $address3 -p 5001 -t $time3 -i 1 -w 64K | tee "/root/iperf-data/$now-tcp-64K-client.txt" } four(){ echo "Set iperf Server UDP" now=$(date +"%F_%H%M%S") LOG_FILE="/root/iperf-data/$now-udp-server.txt" /bin/iperf -s -p 5001 -u > >(tee -a ${LOG_FILE} ) } five(){ echo "Set iperf Client UDP 1000Mbps" now=$(date +"%F_%H%M%S") echo "Remote Address: $address Time: $time" read -e -i "$address2" -p "Enter remote ip address:: " address3 address2="${address3:-$address2}" read -e -i "${time2}" -p "How long do you want to test (in seconds)?:" time3 time2="${time3:-$time2}" read -p "Enter bandwidth to test on (in Mbps):" speed speed2="${speed}" echo "Remote Address: $address3 Time: $time3" /bin/iperf -c $address3 -p 5001 -t $time3 -i 1 -u -b ${speed}m | tee "/root/iperf-data/$now-udp-client.txt" } # function to display menus show_menus() { clear echo "~~~~~~~~~~~~~~~~~~~~~~~~" echo " iperf M A I N - M E N U" echo "~~~~~~~~~~~~~~~~~~~~~~~~" echo "" echo "1. Set iperf Server TCP" echo "2. Set iperf Client 8K" echo "3. Set iperf Client 64K" echo "~~~~~~~~~~~~~~~~~~~~~~~~" echo "4. Set iperf Server UDP" echo "5. Set iperf Client UDP" echo "6. Exit" } # read input from the keyboard and take a action # invoke the one() when the user select 1 from the menu option. # invoke the two() when the user select 2 from the menu option. # Exit when user the user select 3 form the menu option. read_options(){ local choice read -p "Enter choice [ 1 - 6] " choice case $choice in 1) one ;; 2) two ;; 3) three;; 4) four;; 5) five;; 6) exit 0;; *) echo -e "${RED}Error...${STD}" && sleep 2 esac } # ---------------------------------------------- # Step #3: Trap CTRL+C, CTRL+Z and quit singles # ---------------------------------------------- trap '' SIGINT SIGQUIT SIGTSTP # ----------------------------------- # Step #4: Main logic - infinite loop # ------------------------------------ while true do show_menus read_options done