Linux命令行查看当前上传/下载速度 - Go语言中文社区

Linux命令行查看当前上传/下载速度


参考:linux命令行查看实时网速

参考文章在Ubuntu Server 18.04下不适用,在此稍作修改。

OS:Ubuntu Server 18.04

脚本:

$ cat current_net_speed.sh
#!/bin/sh

LANG=""
while true
do
up_time1=`ifconfig $1 | grep "TX packets" | awk '{print $5}'`
down_time1=`ifconfig $1 | grep "RX packets" | awk '{print $5}'`
sleep 1
clear
up_time2=`ifconfig $1 | grep "TX packets" | awk '{print $5}'`
down_time2=`ifconfig $1 | grep "RX packets" | awk '{print $5}'`

up_time=`expr $up_time2 - $up_time1`
down_time=`expr $down_time2 - $down_time1`
up_time=`expr $up_time / 1024`
down_time=`expr $down_time / 1024`
echo 上传速度: $up_time KB/s
echo 下载速度: $down_time KB/s
done

执行:

$ ./current_net_speed.sh enp2s0

ifconfig的结果需要是下面这种样式的。

$ ifconfig
......

enp2s0: flags=xxxx<UP,BROADCAST,RUNNING,MULTICAST>  mtu xxxx
        inet 192.168.0.55  netmask 255.xxx.xxx.xxx  broadcast 192.xxx.xxx.xxx
        inet6 xxxx::xxxx:xxxx:xxxx:xxxx  prefixlen xx  scopeid 0xxx<link>
        ether xx:xx:xx:xx:xx:xx  txqueuelen xxx  (Ethernet)
        RX packets 109476405  bytes 29079102145 (29.0 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 345059512  bytes 503541267103 (503.5 GB)

原理:

通过ifconfig,我们可以查看截止目前已上传/下载的数据量,即下面两行bytes后面的数据,RX为下载的,TX为上传的。

        RX packets 109476405  bytes 29079102145 (29.0 GB)
        TX packets 345059512  bytes 503541267103 (503.5 GB)

那么,我们每秒钟获取一次上传/下载的数据量,其差值就是上传/下载速度了。

 

 

 

 

转载于:https://my.oschina.net/igiantpanda/blog/1841575

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_34292959/article/details/92569289
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-18 22:15:14
  • 阅读 ( 1145 )
  • 分类:Linux

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢