Want to see the Linux traffic situation in real time, and do not want to go to the third-party tools, you can directly write footsteps run!
System: CentOS 6.5
Principle: Obtain the flow from the/proc/net/dev, and then by the conversion and divided by the interval time to get the flow unit m
#------------------#Author: administrator#created: --Geneva-Ten#------------------#!/bin/bash# Usage Method usage () {Echo "useage: $ ethname intervel"Exit0} #网口配置if[$#-GT1]; ThenEthname=$1Num=`ifconfig|grep${ethname}|WC-l 'if[$num-GT0]; ThenEthname=$1 Else Echo "Can ' t find $ethname, please Check"Exit1 fifi#时间配置intervel=1if[$#-GT0]; Theninterval=$2Test=`Expr "$interval"\*0` if["$test"!="0"]; Then Echo "Invalid <interval> param ' $interval '. It should be a integer number. Usage: $ ethname intervel"Exit-1 fi if[$interval-lt1]; Then Echo "Invalid <interval> param ' $interval '. It should be > 0 (seconds). Usage: $ ethname intervel"Exit-1 fifi#函数判断if[$#-ne2 ] ThenusagefiEcho "Starting,device is $1,intervel are $"Echo-E"\ n"typeset Rev Old_reveth=$1Intervel=$2Old_rev=`Cat/proc/net/dev|grep$eth |awk '{print $}'|awk-F:'{print $}'' Old_send=`Cat/proc/net/dev|grep$eth |awk '{print $9}'`#Echo "Old_rev is $old _rev"#Echo "Old_send is $old _send" while true Do Sleep$intervel Rev=`Cat/proc/net/dev|grep$eth |awk '{print $}'|awk-F:'{print $}'' Send=`Cat/proc/net/dev|grep$eth |awk '{print $9}'`#Echo "Rev is $Rev"#Echo "Send is $Send"diff_1=`awk-V rev= $Rev-v old_rev= $old _rev-v intervel= $intervel'begin{printf "Receive is%6.2f", (rev-old_rev)/1024/1024/intervel * 8}'' Diff_2=`awk-V send= $Send-v old_send= $old _send-v intervel= $intervel'begin{printf "Send is%6.2f", (send-old_send)/1024/1024/intervel * 8}'` Echo "' date + '%y-%m-%d%h:%m:%s ' speed:in ${diff_1} mbps out ${diff_2} Mbps"Old_rev=$Rev old_send=$Send Done
If you have problems with the use, please contact:
[Email protected]
Monitoring Linux Traffic shell version