Use shell script to compute the Linux NIC traffic, the most critical point in the method:
Copy Code code as follows:
Ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} '
The flow of input and output is obtained by Ifconfig eth0|grep bytes.
Copy Code code as follows:
/@rac2 =>dd2$ifconfig Eth0|grep bytes
RX bytes:1638005313300 (1.4 TiB) TX bytes:3408060482049 (3.0 TiB)
The results are then used to derive the desired field value from awk.
Fixed time to get these values, in the Write a loop calculation can get the network card traffic.
Complete code:
Code One:
#!/bin/bash #
Statistics network card Traffic
# link:www.jb51.net
# date:2013/2/26
n=10
date
rm-rf/tmp/ifconfig_ Log while
(($n >= 0))
do
n=$ (($n-1));
Date >>/tmp/ifconfig_log
ifconfig eth1 >>/tmp/ifconfig_log sleep
1
done
grep "RX Bytes: "/tmp/ifconfig_log | Awk-f "[: |]" ' {print $} ' | awk ' Begin{tmp=$1}{if (FNR > 1) Print $1-tmp}{tmp=$1} '
Code two:
#!/bin/bash if [-N "$]"; Then eth_name=$1 else eth_name= "eth0" fi i=0 send_o= ' ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} ' recv_o= ' ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} ' send_n= $send _o recv_n= $recv _o while [$i-le 100000]; Do send_l= $send _n recv_l= $recv _n sleep 1 send_n= ' ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} ' recv_n= ' ifconfig $eth _name | grep bytes | awk ' {print $} ' | Awk-f: ' {print $} ' i= ' expr $i + 1 ' send_r= ' expr $send _n-$send _l ' recv_r= ' expr $recv _n-$recv _l ' total_r= ' expr $ Send_r + $recv _r ' send_ra= ' expr \ ($send _n-$send _o \)/$i ' recv_ra= ' expr \ ($recv _n-$recv _o \)/$i ' total_ra= ' exp R $send _ra + $recv _ra ' sendn= ' ifconfig $eth _name | grep bytes | Awk-f \ (' {print $} ' | awk-f \] ' {print $} ' recvn= ' ifconfig $eth _name | grep bytes | Awk-f \ (' {print $} ' | awk-f \) ' {print} ' clear Echo ' ================================================ = "echo" Last Second:send rate: $send _r bytes/sec Recv rate: $RECV _r bytes/sec Total rate: $total _r bytes/sec "Ech o "Average value:send rate: $send _ra bytes/sec Recv rate: $RECV _ra bytes/sec Total rate: $total _ra bytes/sec" echo "Tot Al traffic after Startup:send traffic: $sendn RECV traffic: $recvn "echo" ============================================= ===== "Done
Code Three:
#!/bin/bash
# link:www.51bbo.com
### while
:
do
time= ' date +%f ' "%t.%n '
rx_before= '" Ifconfig eth0 |sed-n 8p |awk ' {print $} ' | cut-c7-'
tx_before= ' ifconfig eth0 |sed-n 8p |awk ' {print $} ' | cut-c7-' sleep
2
rx_after= ' ifconfig eth0 D-n 8p |awk ' {print $} ' | cut-c7-'
tx_after= ' ifconfig eth0 |sed-n 8p |awk ' {print $} ' | cut-c7-'
rx_result=$[(rx_after–rx_before)/512 ]
tx_result=$[(tx_after–tx_before)/512]
echo-e "$Time nnow_in_speed: ' $rx _result ' Kbps now_out_speed: ' $tx _result ' kbpsn ' done