實用的網絡流量監控腳本

     經過這個腳本net.sh咱們能夠看到某個網卡的收發報文流量概況,在某些狀況下bash

能夠用來進程網絡程序的參數調優。網絡

    

 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
運行結果以下圖所示:ide

相關文章
相關標籤/搜索