1,Iperf 等工具,需要安装服务端

https://blog.csdn.net/qq_15437629/article/details/78827152

2,speedtest 需要python

https://blog.csdn.net/h952520296/article/details/78458072

 

3, 使用c语言编写,直接获取/proc/net/dev中流量数据。 被动获取

https://blog.csdn.net/ljy520yzy/article/details/7558933

/*****************************************************
*
* 作者:杨志永
* 日期:2012-4-16 4:35PM
* E-mail:ljy520zhiyong@163.com
* QQ:929168233
*
* filename: watch_net_speed.c
* 编译环境:Debian 6.0.4 Testing + GCC 4.6.3 X86_64
*
*****************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
 
#define WAIT_SECOND 5	//暂停时间,单位为“秒”  
 
long int getCurrentDownloadRates(long int * save_rate); //获取当前的流量,参数为将获取到的流量保存的位置  
 
int main(int argc, char * argv[])
{
    long int start_download_rates;	//保存开始时的流量计数  
    long int end_download_rates;	//保存结果时的流量计数  
    while(1)
    {
        getCurrentDownloadRates(&start_download_rates);//获取当前流量,并保存在start_download_rates里  
        sleep(WAIT_SECOND);	//休眠多少秒,这个值根据宏定义中的WAIT_SECOND的值来确定 
        getCurrentDownloadRates(&end_download_rates);//获取当前流量,并保存在end_download_rates里  
        printf("download is : %.2lf Bytes/s\n", (float)(end_download_rates-start_download_rates)/WAIT_SECOND );//打印结果  
    }
    exit(EXIT_SUCCESS);
}
 
long int getCurrentDownloadRates(long int * save_rate)
{
    FILE * net_dev_file;	//文件指针  
    char buffer[1024];	//文件中的内容暂存在字符缓冲区里  
    size_t bytes_read;	//实际读取的内容大小   
    char * match;	 //用以保存所匹配字符串及之后的内容  
    if ( (net_dev_file=fopen("/proc/net/dev", "r")) == NULL ) //打开文件/pro/net/dev/,我们要读取的数据就是它啦  
    {
        printf("open file /proc/net/dev/ error!\n");
        exit(EXIT_FAILURE);
    }
    bytes_read = fread(buffer, 1, sizeof(buffer), net_dev_file);//将文件中的1024个字符大小的数据保存到buffer里   
    fclose(net_dev_file); //关闭文件  
    if ( bytes_read == 0 )//如果文件内容大小为0,没有数据则退出  
    {
        exit(EXIT_FAILURE);
    }
    buffer[bytes_read] = '\0';
    match = strstr(buffer, "eth0:");//匹配eth0第一次出现的位置,返回值为第一次出现的位置的地址  
    if ( match == NULL )
    {
        printf("no eth0 keyword to find!\n");
        exit(EXIT_FAILURE);
    }
    sscanf(match, "eth0:%ld", save_rate);//从字符缓冲里读取数据,这个值就是当前的流量啦。呵呵。  
    return *save_rate;
}

4,shell脚本方式,通过ifconfig命令获取tx,rx数据。 被动获取

https://www.right.com.cn/forum/thread-105037-1-1.html 

此文中ifconfig格式跟centos不一样

centos需要调整取数据部分

#!/bin/bash
#
# shaozx@gmail.com  2010-10-19
#
# Usage:
#    ./netspeed.sh             runs 60 seconds by step 1s, default to eth0
#    ./netspeed.sh eth1 10     runs 10 seconds by step 1s, default to eth1
#    ./netspeed.sh eth1 90 5   runs 90 seconds by step 5s, default to eth1


IF="eth0"     # net card device
TIME=60       # run $TIME seconds before quit
STEP=1        # wait $STEP seconds
IFCONF="/sbin/ifconfig"
[ ! -x "/sbin/ifconfig" ] &&
        IFCONF=`type ifconfig | awk '{print $NF}' | sed 's/(\|)//g'`

[ "$1" != "" ] && IF=$1
[ "$2" != "" ] && TIME=$2
[ "$3" != "" ] && STEP=$3


## ========================================= ##
##           DO NOT NEED CHANGE              ##
## ========================================= ##

export LC_ALL=C

show_sum()
{
        #printf "\n%s\n" "[`date +'%Y-%m-%d %H:%M:%S'`](END)"
        echo 
        echo
        #printf "%d seconds,\n" $RUN
        #printf "rx from %d to %d\n" $RX_I $RX
        #printf "tx from %d to %d\n" $TX_I $TX
        echo   '-----------------------------------'
        printf "|    |    TOTAL(K)   |   AVG(K/S) |\n"
        echo   '|---------------------------------|'
        printf "|RX  |%12d   |%10d  |    runs %d seconds\n"  \
                $(( (RX-RX_I)/1024 )) $(( (RX-RX_I)/1024/$RUN )) \
                $RUN
        printf '|---------------------------------|    rx from %d to %d\n' \
                $RX_I $RX
        printf "|TX  |%12d   |%10d  |    tx from %d to %d\n" \
                $(( (TX-TX_I)/1024 )) $(( (TX-TX_I)/1024/$RUN )) \
                $TX_I  $TX
        echo   '-----------------------------------'
        exit 0
}

trap show_sum INT
# stty intr ^C

TX_OLD=0
RX_OLD=0

echo 
echo "   IF: $IF     TIME: ${TIME}s (step=${STEP}s) (down/up)"
echo '----------------------------------------------------------'

while [ 1 ]
do
        LINE=`$IFCONF $IF | grep "RX packets"`
        RX=`echo $LINE | cut -d ' ' -f5 | awk '{print $1}'`

        LINE=`$IFCONF $IF | grep "TX packets"`
        TX=`echo $LINE | cut -d ' ' -f5 | awk '{print $1}'`

	if [ "$RX_OLD" -eq "0" -a "$TX_OLD" -eq "0" ] 
        then
                RX_I=$RX; TX_I=$TX; RX_OLD=$RX; TX_OLD=$TX; RUN=0
                sleep $STEP
                continue
        fi

        RUN=$((RUN+STEP))
        if [ $RUN -ge $TIME ]
        then
                show_sum
                break
        fi

        RX_SUB=`expr $RX - $RX_OLD`
        TX_SUB=`expr $TX - $TX_OLD`
        RX_SUB=`expr $RX_SUB / $STEP / 1024`
        TX_SUB=`expr $TX_SUB / $STEP / 1024`

        RX_OLD=$RX
        TX_OLD=$TX

        printf "%s%-10s" "[`date +'%Y-%m-%d %H:%M:%S'`]" "($((TIME-RUN)))"
        printf "%10d%10d (K/S)\n" $RX_SUB $TX_SUB

        sleep $STEP

done

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐