实用的网络流量监控脚本

     通过这个脚本net.sh我们可以看到某个网卡的收发报文流量概况,在某些情况下

可以用来进程网络程序的参数调优。

    

 1 #!/bin/bash  
 2 if [ -n "$1" ]; then  
 3     eth_name=$1  
 4 else  
 5     eth_name="eth0" 
 6 fi  
 7 i=0 
 8 send_o=`/sbin/ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`  
 9 recv_o=`/sbin/ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`  
10 send_n=$send_o  
11 recv_n=$recv_o  
12 while [ $i -le 100000 ]; do  
13     send_l=$send_n  
14     recv_l=$recv_n  
15 sleep 1  
16 send_n=`/sbin/ifconfig $eth_name | grep bytes | awk '{print $6}' | awk -F : '{print $2}'`  
17 recv_n=`/sbin/ifconfig $eth_name | grep bytes | awk '{print $2}' | awk -F : '{print $2}'`  
18 i=`expr $i + 1`  
19 send_r=`expr $send_n - $send_l`  
20 recv_r=`expr $recv_n - $recv_l`  
21 total_r=`expr $send_r + $recv_r`  
22 send_ra=`expr \( $send_n - $send_o \) / $i`  
23 recv_ra=`expr \( $recv_n - $recv_o \) / $i`  
24 total_ra=`expr $send_ra + $recv_ra`  
25 sendn=`/sbin/ifconfig $eth_name | grep bytes | awk -F \( '{print $3}' | awk -F \) '{print $1}'`  
26 recvn=`/sbin/ifconfig $eth_name | grep bytes | awk -F \( '{print $2}' | awk -F \) '{print $1}'`  
27 clear  
28 echo  "Last second  :   Send rate: $send_r Bytes/sec  Recv rate: $recv_r Bytes/sec  Total rate: $total_r Bytes/sec"  
29 echo  "Average value:   Send rate: $send_ra Bytes/sec  Recv rate: $recv_ra Bytes/sec  Total rate: $total_ra Bytes/sec"  
30 echo  "Total traffic after startup:    Send traffic: $sendn  Recv traffic: $recvn"  
31 done
View Code

    直接./net.sh运行的话默认是网卡eth0, 如果需要看其它的网卡运行./net.sh eth1
运行结果如下图所示:

posted @ 2014-01-08 16:15  cstar(小乐)  阅读(734)  评论(0编辑  收藏  举报