Two-segment shell script that counts network card traffic (using ifconfig) _linux shell

Source: Internet
Author: User

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  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.