Linux 脚本 实现网络流量监控

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

你可能感兴趣的:(Linux 脚本 实现网络流量监控)