linux下监控网络流量的脚本

所属分类: 脚本专栏 / linux shell 阅读数: 2098
收藏 0 赞 0 分享
我看了下,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
更多精彩内容其他人还在看

关于ssh连不上问题的解决方法(必看)

下面小编就为大家带来一篇关于ssh连不上问题的解决方法(必看)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Ubuntu用户之间相互切换方法(推荐)

下面小编就为大家带来一篇Ubuntu用户之间相互切换方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Linux 编程之进程fork()详解及实例

这篇文章主要介绍了Linux 编程之进程fork()详解及实例的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

浅谈安装ORACLE时在Linux上设置内核参数的含义

下面小编就为大家带来一篇浅谈安装ORACLE时在Linux上设置内核参数的含义。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详谈linux中sar的使用方法

下面小编就为大家带来一篇详谈linux中sar的使用方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

设置Linux系统的空闲等待时间TMOUT的方法

下面小编就为大家带来一篇设置Linux系统的空闲等待时间TMOUT的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

关于Linux反空闲设置的两种方法总结

下面小编就为大家带来一篇关于Linux反空闲设置的两种方法总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

linux 查找大目录和大文件的方法(推荐)

下面小编就为大家带来一篇linux 查找大目录和大文件的方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Linux makefile 和shell文件相互调用实例详解

这篇文章主要介绍了Linux makefile 和shell文件相互调用实例详解的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Windows与Linux共享文件夹互相访问

这篇文章主要介绍了 详解Windows与Linux共享文件夹互相访问的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多