博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
写了个监控网络流量的脚本
阅读量:3604 次
发布时间:2019-05-20

本文共 1633 字,大约阅读时间需要 5 分钟。

在我们的日常工作当中,监控网络要么使用iptarf,ifstat这些命令实现的,但是需要装额外的RPM包。特别是iptarf装起来麻烦。

我看了下,linux下的/proc/net/dev记录了每块网卡发送和接受的包和字节数。因此萌生想法,写了一个。运行效果:

root@:/root/wt>sh  aa.sh

Current Ip:   inet addr:10.0.65.52 Bcast:10.0.65.255 Mask:255.255.255.0
Summry info:  RX bytes:2424183819 (2311.8 Mb) TX bytes:3519850565 (3356.7 Mb)
eth0 Receive Bytes: 61147  Packets: 433
eth0 Send Bytes: 86458  Packets: 372
eth0 Receive Bytes: 156051  Packets: 924
eth0 Send Bytes: 230962  Packets: 877
eth0 Receive Bytes: 192537  Packets: 1118
eth0 Send Bytes: 283893  Packets: 1073

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

root@:/root/wt>cat aa.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

转载地址:http://joizn.baihongyu.com/

你可能感兴趣的文章
设计模式之享元模式
查看>>
设计模式之组合模式
查看>>
设计模式之委派模式
查看>>
设计模式之模板方法模式
查看>>
设计模式之策略模式
查看>>
设计模式之责任链模式
查看>>
怎么成为一个合格的ERP系统管理员
查看>>
企业为什么要用ERP
查看>>
ERP计划层次探讨
查看>>
ERP的五大核心思想
查看>>
ERP、PLM是什么意思?ERP、PLM有什么内在联系
查看>>
公司升级ERP管理系统的三大诱因
查看>>
Android四大应用组件(一)——Activity
查看>>
Spring5框架——IOC(基于xml配置文件)(一)
查看>>
spring5框架——IOC(基于注解)&AOP(二)
查看>>
SpringMVC框架(一)
查看>>
SpringMVC框架(二)
查看>>
IDEA整合SSM框架
查看>>
Spring注解驱动开发(一)
查看>>
Spring注解驱动开发(二)
查看>>