码迷,mamicode.com
首页 > 系统相关 > 详细

linux iperf3 一键自动化测试脚本

时间:2015-09-01 10:53:54      阅读:1029      评论:0      收藏:0      [点我收藏+]

标签:iperf3 linux 测试 带宽

iperf3 一键自动化测试脚本:


日    期:2015年09月01日
联系邮箱:linux_lcl@163.com
Q Q  群:1851 15701
51CTO博客首页:http://990487026.blog.51cto.com


准备:

1,已经安装iperf3

2,root权限


功能:

1,自动测试ping的数据: ping延时 与ping 丢包,30次

2,iperf3 自动测试TCP 上行带宽吞吐量,单位Kbits/sec

3,iperf3 自动测试TCP 下行带宽吞吐量,单位Kbits/sec

4,iperf3 自动测试UDP的丢包与抖动

5,自动判断iperf3 是否处于僵死状态,如果是,那么就杀了它,每隔300秒检查一次

6,自动判断是否得到了想到的数据值,如果没有得到,就会重试,直到得到数据为止

7,参数指定:

# $1 目标IP地址
# $2  脚本运行时长, 单位:秒
# $3 iperf3 -t 的参数

# $4 sleep 脚本每次运行,休息时长 单位: 秒

使用方法:

./iperf14.sh baidu.com 99999 5 10


6,运行脚本会生成.csv数据报表,可以用office软件中的excel直接查看数据

格式如下:

Localhost, 192.**.**.**
Remote,54.**.**.**
Bandwidth Kbits/sec

Date,Start_Time,End_time,Up_Sender,Up_Receiver,Down_Sender,Down_Receiver,Jitter/ms,Lost/Total,Ping_Latency/ms,Ping_Lost
2015-09-01,08:20:44,08:21:38,799,145,359,359,0.147,11%,511.811,0% 
2015-09-01,08:21:39,08:22:42,799,145,683,683,0.209,12%,529.747,0% 
2015-09-01,08:22:43,08:23:43,1610,295,556,556,0.190,7.9%,501.740,0% 
2015-09-01,08:23:44,08:24:35,608,156,568,568,0.169,11%,496.672,0% 
2015-09-01,08:24:36,08:25:35,602,162,567,567,0.377,12%,509.504,0% 
2015-09-01,08:25:36,08:26:27,1483,295,707,707,0.278,6.7%,507.316,0%


可以对采集到的数据画折线图,


====================================================================

root@debian:/iperf.sh/files# cat iperf14.sh 
#!/bin/bash
# modify 2015.08.31 14:05
# up ,down Bandwidth unit is Kbits/sec
# $1 for ip address
# $2 .sh run time
# $3 iperf3 -t time
# $4 sleep seconds
# add ping_lost 
# retry fun
# kill iperf sleep 5
# fix nan error 
# add Supervision iperf3 
# add judgement root
# fix File_Modify == 
#### judgement root ######################################################################
root_id=`id -u`
if [ $root_id -ne 0 ] ; then
{
   clear
   echo -e "\033[40;37mWarning: you are not root user ! \n\n[Please use Command line ]$ sudo su \n\n \033[0m"
   exit 10
}
fi
####### Get localhost IP & Initialization for iperf.csv############################3
clear
time4=`date +‘%s‘`
time5=$[time4+$2] 
mkdir -p files
time1=`date "+%F-%H-%M"`
iperf="iperf3 -c $1 -t $3 -f k -p 10000"
ifconfig eth0 > files/tmp1
localIP=`sed -n "2p" files/tmp1|awk ‘{print $2}‘|cut -d: -f2`
echo "Localhost,$localIP" >>  ./files/${time1}_iperf.csv
echo "Remote,$1" >> ./files/${time1}_iperf.csv
echo "Bandwidth Kbits/sec" >>  ./files/${time1}_iperf.csv
echo "" >>  ./files/${time1}_iperf.csv
echo
 
"Date,Start_Time,End_time,Up_Sender,Up_Receiver,Down_Sender,Down_Receiver,Jitter/ms,Lost/Total,Ping_Latency/ms,Ping_Lost"
 >> ./files/${time1}_iperf.csv

##### Supervision iperf3 #############################################
while [ $time4 -le $time5 ]
do
     sleep1=300
     File_Modify1=`stat ./files/tmp1 | tail -n 2| head -n 1| awk ‘{print $3}‘`
     sleep $sleep1 
     File_Modify2=`stat ./files/tmp1 | tail -n 2| head -n 1| awk ‘{print $3}‘`
     if [ $File_Modify1 == $File_Modify2 ]
     then
          killall iperf3 >> /dev/null 2>&1
          sleep 1
          killall iperf3 >> /dev/null 2>&1
          echo -e "`date`"
          echo -e "\n\n\n\033[43;31m kill zombie iperf3 ..............\033[0m"
          sleep 1

     else
          echo -e "\033[43;31m iperf3 Runing,Has not become zombie ,Every $sleep1 Seconds Check Once  .........\033[0m"
     fi
time4=`date +‘%s‘`
done &

# Begin ##############################################
while [ $time4 -le $time5 ]
do
    time2=`date "+%F"`            # Get current time
    time3=`date "+%H:%M:%S"`
   echo "##########################################################################"
   echo -e "`date`"
   echo "Get ping information .....ping -c 30 $1"
    ping -c 30 $1 | tee ./files/tmp1 
    tail -2 files/tmp1 > ./files/tmp2
    ping_Latency=`sed -n "2p" ./files/tmp2 | cut -d/ -f5`
    ping_lost=`sed -n "1p" ./files/tmp2 | awk ‘{print $6}‘`
    echo -e "`date`"
    echo -e "\033[43;31mping_delay:$ping_Latency ms \033[0m"
    echo -e "\033[43;31mping_lost:$ping_lost \033[0m"
  echo "##########################################################################"
  echo -e "\n\n`date`"
  echo "Get upload information........ $iperf "
    $iperf | tee ./files/tmp1
    grep "sender" files/tmp1
    stat=`echo $?`
    grep "iperf Done" files/tmp1
    stat_done=`echo $?`
       while ( [ $stat != 0 ] || [ $stat_done != 0 ] ) 
       do
         killall iperf3 >> /dev/null 2>&1
         sleep 2
         killall iperf3 >> /dev/null 2>&1
         echo -e "`date`"
         echo "Retry Get upload information........ $iperf  "
         sleep 5
         $iperf  | tee ./files/tmp1
         grep "sender" files/tmp1
         stat=`echo $?`
         grep "iperf Done" files/tmp1
         stat_done=`echo $?`
       done
    tail -4 ./files/tmp1 > ./files/tmp2
      up_sender=`sed -n "1p" ./files/tmp2 | awk ‘{print $7}‘`
    up_receiver=`sed -n "2p" ./files/tmp2 | awk ‘{print $7}‘`
    echo -e "\033[43;31mup_sender: $up_sender Kbits/sec\033[0m"
    echo -e "\033[43;31mup_receiver:$up_receiver Kbits/sec\033[0m"
  echo "##########################################################################"
  echo "Get download information........ $iperf -R  "
    $iperf -R | tee ./files/tmp1
    grep "sender" files/tmp1
    stat=`echo $?`
    grep "iperf Done" files/tmp1
    stat_done=`echo $?`
       while ( [ $stat != 0 ] || [ $stat_done != 0 ] ) 
       do
         killall iperf3 >> /dev/null 2>&1
         sleep 2
         killall iperf3 >> /dev/null 2>&1
         echo -e "`date`"
         echo "Retry Get download information........ $iperf -R  "
         sleep 5
         $iperf -R | tee ./files/tmp1
         grep "sender" files/tmp1
         stat=`echo $?`
         grep "iperf Done" files/tmp1
         stat_done=`echo $?`
       done
    tail -4 ./files/tmp1 > ./files/tmp2
      down_sender=`sed -n "1p" ./files/tmp2 | awk ‘{print $7}‘`
    down_receiver=`sed -n "2p" ./files/tmp2 | awk ‘{print $7}‘`
    echo -e "\033[43;31mdown_sender:$down_sender Kbits/sec\033[0m"
    echo -e "\033[43;31mdown_receiver:$down_receiver Kbits/sec\033[0m"
  echo "##########################################################################"
  echo -e "Get UDP information...... $iperf -u -l 1400 "
    $iperf -u -l 1400  | tee ./files/tmp1
    grep "Sent" ./files/tmp1
    stat=`echo $?`
    grep "nan" files/tmp1
    stat2=`echo $?`
    grep "iperf Done" files/tmp1
    stat3=`echo $?`
    if [ $stat -eq 0 ]
   then
        if [ $stat2 -eq 0 -o $stat3 -ne 0 ]
        then
           stat=1    
        fi
    fi
       while [[ $stat != 0 ]]
       do
         killall iperf3 >> /dev/null 2>&1
         sleep 1
         killall iperf3 >> /dev/null 2>&1
         echo -e "`date`"
         echo "Retry Get UDP information........  $iperf -u -l 1450 "
         sleep 1
         $iperf -u -l 1400  | tee ./files/tmp1
         grep "Sent" ./files/tmp1
         stat=`echo $?`
         grep "nan" files/tmp1
         stat2=`echo $?`
         grep "iperf Done" files/tmp1
         stat3=`echo $?`
         if [ $stat -eq 0 ]
         then
         if [ $stat2 -eq 0 -o $stat3 -ne 0 ]
             then
                stat=1    
             fi
         fi
       done
    tail -4 ./files/tmp1 > ./files/tmp2
    Jitter=`sed -n "1p" ./files/tmp2 | awk ‘{print $9}‘`
    sed -n "1p" ./files/tmp2  |awk ‘{print $12}‘>./files/tmp1
    sed -i "s#(##g" ./files/tmp1
    sed -i "s#)##g" ./files/tmp1  
    Lost=`cat ./files/tmp1`
    echo -e "\033[43;31mJitter : $Jitter\033[0m"
    echo -e "\033[43;31mLost: $Lost \033[0m"
    echo "#########################################################################"
    time_end=`date "+%H:%M:%S"`
    echo -e "\033[43;31mlocal  IP is $localIP \033[0m"
    echo -e "\033[43;31mremote Ip is $1 \033[0m"
   
 echo 
"$time2,$time3,$time_end,$up_sender,$up_receiver,$down_sender,$down_receiver,$Jitter,$Lost,$ping_Latency,$ping_lost
 " >> ./files/${time1}_iperf.csv
    echo -e "\033[43;31mrecoder file save as  ./files/${time1}_iperf.csv \033[0m"
    echo -e "sleep  $4 seconds #####################################################\n\n\n\n"   
    sleep $4
time4=`date +‘%s‘`
done 

rm -rf ./files/tmp1
rm -rf ./files/tmp2
clear 
echo -e "\033[31m*******************************************************************\033[0m"
echo -e "Information Collection Complete !                        "
date
echo -e "recoder file save as  ./files/${time1}_iperf.csv                 "
echo -e "\033[31m*******************************************************************\033[0m"

===================================================================================




本文出自 “生命不息,折腾不止。” 博客,请务必保留此出处http://990487026.blog.51cto.com/10133282/1690371

linux iperf3 一键自动化测试脚本

标签:iperf3 linux 测试 带宽

原文地址:http://990487026.blog.51cto.com/10133282/1690371

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!