Ubuntu测网速

Ubuntu测网速

我们利用ifconfig里发接受字节发送字节,分两次取值然后在除以间隔时间,获得那段时间的网速ifconfig参数
代码:
up_time1 :发送字节 ====》 上传
down_time1 : 接受字节 ====》下载

#! /bin/bash

LANG=""
while true
do
   #第一次获取的字节
    up_time1=`ifconfig $1 | grep "bytes" | awk '{print $6}' | head -n1 | tr -cd "[0-9]"`
    down_time1=`ifconfig $1 | grep "bytes" | awk '{print $2}' | head -n1 | tr -cd "[0-9]"`

    #间隔时间
    time_intval=5  #$2 
    zero=0
    #睡眠
    sleep $time_intval
    clear
    echo netspeed start... 
    #第二次获取的字节
    up_time2=`ifconfig $1 | grep "bytes" | awk '{print $6}' | head -n1 | tr -cd "[0-9]"`
    down_time2=`ifconfig $1 | grep "bytes" | awk '{print $2}' | head -n1 | tr -cd "[0-9]"`
    
    up_time1=${up_time1}
    #echo up_time1: $up_time1 
    up_time2=${up_time2}
    #echo up_time2: $up_time2
    down_time1=${down_time1}
    #echo down_time1: $down_time1
    down_time2=${down_time2}
    #echo down_time2: $down_time2
    
    up_time_temp=`expr $up_time2 - $up_time1`
    up_time=`expr $up_time_temp / $time_intval`
    echo $up_time
    down_time_temp=`expr $down_time2 - $down_time1`
    down_time=`expr $down_time_temp / $time_intval`
    echo $down_time

    up_time_head=`expr $up_time / 1024  `
    down_time_head=`expr $down_time / 1024  `
    # if [$up_time -eq 0]
    # then 
    #     up_time_tail= 0
    #     down_time_tail=`expr $down_time % 1024 `
    # fi
    # if [$down_time -eq 0]
    # then
    #     up_time_tail= `expr $up_time % 1024  `
    #     down_time_tail=0    
    # fi


    echo upload speed: $up_time_head KB/s
    echo download speed: $down_time_head KB/s

    # echo   upload speed: $up_time_head.$up_time_tail KB/s
    # echo download speed: $down_time_head.$down_time_tail KB/s
done
解释 ifconfig $1 | grep "bytes" | awk '{print $6}' | head -n1 | tr -cd "[0-9]" 的命令
  • ifconfig $1 | grep "bytes" :获取ifconfig里有关 "bytes"的信息。
  • awk '{print $6}' | head -n1 :获取打印并且只要第一行的的{$6}的信息
  • tr -cd "[0-9]" :只要数字信息
    因此此命令最终会获得第一张图的发送字节的数字:36707539

如有不正确,请予以指导,将会很快做出改正

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值