linux下监控网络流量的脚本
程序员文章站
2022-05-24 17:58:10
我看了下,linux下的/proc/net/dev记录了每块网卡发送和接受的包和字节数。因此萌生想法,写了一个。运行效果: 复制代码 代码如下: [root@74-82-1...
我看了下,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
复制代码 代码如下:
[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
推荐阅读
-
python使用wmi模块获取windows下的系统信息 监控系统
-
在Linux 服务器下修改防火墙允许该端口远程访问的方法
-
Linux下Redis允许远程连接的实现方法
-
Linux下安装PHP curl扩展的方法详解
-
Linux工具之Nethogs按进程监控网络带宽的安装部署
-
Linux (Ubuntu 18.04) 下安装vim编辑器的方法
-
对linux下syslogd以及syslog.conf文件的解读说明
-
Linux下解压.tar.xz格式文件的方法
-
linux下使用crontab实现定时PHP计划任务失败的原因分析
-
解决Linux下ssh登录后出现 报错 Write failed: Broken pipe 的方法