shell统计网卡流量速度

[root@orabi opt]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:1D:09:08:A3:BB  
          inet addr:192.168.1.197  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::21d:9ff:fe08:a3bb/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:258200 errors:0 dropped:0 overruns:0 frame:0
          TX packets:82533 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:126625034 (120.7 MiB)  TX bytes:8552959 (8.1 MiB)
          Interrupt:33

统计的原理就是截取“RX bytes:126625034 (120.7 MiB) TX bytes:8552959 (8.1 MiB)”,通过sleep后,计算该段事件内的差值。

附上代码:

#!/bin/bash 
ETH=eth0
while : ; do
time=`date +%m"-"%d" "%k":"%M`
day=`date +%m"-"%d`
rx_before=`ifconfig $ETH|sed -n "8"p|awk '{print $2}'|cut -c7-`
tx_before=`ifconfig $ETH|sed -n "8"p|awk '{print $6}'|cut -c7-`
sleep 1
rx_after=`ifconfig $ETH|sed -n "8"p|awk '{print $2}'|cut -c7-`
tx_after=`ifconfig $ETH|sed -n "8"p|awk '{print $6}'|cut -c7-`
rx_result=$[(rx_after-rx_before)/(1024*1)]
tx_result=$[(tx_after-tx_before)/(1024*1)]
echo "$time Now_In_Speed: "$rx_result"kB/s Now_OUt_Speed: "$tx_result"kB/s" 
#if [ $rx_result -gt 10000 ] || [ $tx_result -gt 10000 ]
#then
#   echo "$time Now_In_Speed: "$rx_result"kB/s Now_OUt_Speed: "$tx_result"kB/s">mail 
#   netstat -anlep>>mail
#   mail   -s   "subject"   xxx@189.cn   <mail  
#fi
sleep 1
done
脚本中“#”号注释掉部分,可修改下,并判断峰值,发送邮件到189邮箱功能。

部分系统由于ifconfig eth0出来的结果格式可能有细微差别,上述脚本中参数7和8可能需要微调下。

测试了下wget --limist-rate=100k http://www.xxxx.com/test.zip限速100k下载,结果如下图,基本差不多。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值