• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

linux下监控网络流量的脚本

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
我看了下,linux下的/proc/net/dev记录了每块网卡发送和接受的包和字节数。因此萌生想法,写了一个。运行效果:

复制代码 代码如下:

[root@74-82-173-217 ~]# ./net.sh
Current Ip: inet addr:74.82.173.217 Bcast:74.82.173.223 Mask:255.255.255.224
Summry info: RX bytes:203692709 (194.2 MiB) TX bytes:93525930 (89.1 MiB)
eth0 Receive Bytes: 573 Packets: 3
eth0 Send Bytes: 3086 Packets: 3
eth0 Receive Bytes: 378 Packets: 7
eth0 Send Bytes: 11236 Packets: 7
eth0 Receive Bytes: 324 Packets: 6
eth0 Send Bytes: 444 Packets: 2
eth0 Receive Bytes: 54 Packets: 1
eth0 Send Bytes: 0 Packets: 0


具体脚本的内容如下,几乎不需要修改,就可以拿到任何机器上去使用了。
复制代码 代码如下:

[root@74-82-173-217 ~]# cat net.sh
#! /bin/bash
#Author: Vogts WangTao 2008-12-18
#Get summry info
echo "Current Ip: "`/sbin/ifconfig eth0 | grep inet`
echo "Summry info: "`/sbin/ifconfig eth0 | grep bytes`
#sleep 1 second ,monitor eth0
while true
do
receive1=`cat /proc/net/dev|grep eth0 | awk '{print$1}'|sed -s 's/eth0://g'`
receive_pack1=`cat /proc/net/dev|grep eth0 | awk '{print$2}'`
send1=`cat /proc/net/dev|grep eth0 | awk '{print$9}'`
send_pack1=`cat /proc/net/dev|grep eth0 | awk '{print$10}'`
sleep 1
receive2=`cat /proc/net/dev|grep eth0 | awk '{print$1}'|sed -s 's/eth0://g'`
receive_pack2=`cat /proc/net/dev|grep eth0 | awk '{print$2}'`
receive_cnt=`expr $receive2 - $receive1`
receive_pack_cnt=`expr $receive_pack2 - $receive_pack1`
send2=`cat /proc/net/dev|grep eth0 | awk '{print$9}'`
send_pack2=`cat /proc/net/dev|grep eth0 | awk '{print$10}'`
send_cnt=`expr $send2 - $send1`
send_pack_cnt=`expr $send_pack2 - $send_pack1`
echo 'eth0 Receive Bytes:' $receive_cnt ' Packets:' $receive_pack_cnt
echo 'eth0 Send Bytes:' $send_cnt ' Packets:' $send_pack_cnt
done

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
深入浅出Shell编程 Shell变量介绍发布时间:2022-02-11
下一篇:
linux shell命令的常用快捷键发布时间:2022-02-11
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap