linux shell网卡流量,好玩的网卡流量实时监控脚本 【Bash Shell】

复制内容到剪贴板

代码:#!/bin/bash

#Owner:LingYi

#Date: 2014.04.16

#Environment settings

stty erase ^H;stty erase ^?

trap "tput reset;exit 0" 2

#Variable settings

#Warn:if you are not sure of their role,don't modify!

#variables "Speed_alarm_Rx" and "Speed_alarm_Tx" are the alarm limits,

#you can Modify them according to your own needs.

#time:second; speed:KB/s

Cls_interval=6

Speed_alarm_Rx=1024

Speed_alarm_Tx=1024

Alarm_times=2

Default_nic=eth0

Get_flow_freq=1

Exact_num=4

#For funny

symbol_num=5

def_symbol_l='->'

def_sysbol_r='

def_string="[ It is made by LingYi ~ :) ]"

print_time=0.2

print_time_2=0.3

sleep_time=10

#When you use it, change it back to the string in the background color.

#For example: bcolor -n --red $your_string

# the parameter "-n" means no Enter.you must use "-n" like this.

bcolor(){

if [ $1 == '-n' ];then

color=$2

else

color=$1

fi

case $color in

--red) NUM=41 ;;

--green) NUM=42 ;;

--orange) NUM=43 ;;

--blue) NUM=44 ;;

--pink) NUM=45 ;;

--lit-blue) NUM=46 ;;

*) NUM=41 ;;

esac

strings=`echo $*|sed "s/$color//"|sed 's/-n//'|sed 's/^ *//g'`

if [ $1 == '-n' ];then

echo -n -e "\e[1;${NUM}m$strings\e[0m"

else

echo -e "\e[1;${NUM}m$strings\e[0m"

fi

}

#And you can use it like using the function "bcolor".

fcolor(){

if [ $1 == '-n' ];then color=$2

else color=$1 ;fi

case $color in

--red) NUM=31 ;;

--green) NUM=32 ;;

--orange) NUM=33 ;;

--blue) NUM=34 ;;

--pink) NUM=35 ;;

--lit-blue) NUM=36 ;;

*) NUM=31 ;;

esac

strings=`echo $*|sed "s/$color//"|sed 's/-n//'|sed 's/^ //g'`

if [ $1 == '-n' ];then

echo -n -e "\e[1;${NUM}m$strings\e[0m"

else

echo -e "\e[1;${NUM}m$strings\e[0m"

fi

}

#Funny :)~

#for example:print_char -t 0.1 "your string" (-t: second)

print_char(){

string=`echo $*|sed "s/$1//"|sed "s/$2//"|sed 's/^ *//'|sed 's/ /K/g'`

leng=`echo $string|wc -m`

for ((i=1;i<=$leng;i++));do

echo -n `echo $string|cut -c $i`|sed 's/K/ /'

sleep $2

done

}

#for example: print_bar 100 0.2 you_symbol (The units are seconds)

print_bar(){

NUM=$1

SLEEP_TIME=$2

i=1

while [ $i -le $NUM ];do

n=`echo $(( ${i}%5 )) + 1|bc`

N=3${n}

echo -n -e "\e[1;${N}m${3}\e[0m"

sleep $SLEEP_TIME

let i++

done

}

#Alarm function

Alarm_Rx(){

Judge_Rx=`awk -v num1=$1 -v num2=$Speed_alarm_Rx 'BEGIN{print(num1>=num2)?"0":"1"}'`

if [ $Judge_Rx -eq 0 ];then

for((i=1;i<=Alarm_times;i++)); do tput bel;sleep 1;done &

fi

}

Alarm_Tx(){

Judge_Tx=`awk -v num1=$1 -v num2=$Speed_alarm_Rx 'BEGIN{print(num1>=num2)?"0":"1"}'`

if [ $Judge_Tx -eq 0 ];then

for((i=1;i<=Alarm_times;i++)); do tput bel;sleep 1; done &

fi

}

# Print Interesting string

clear;tput cup 1 5;tput ed

print_bar $symbol_num $print_time $def_symbol_l;print_char -t $print_time_2 " $def_string "

print_bar $symbol_num $print_time $def_sysbol_r

if [ $sleep_time -gt 1 ];then

tput civis

for((;sleep_time>=1;sleep_time--));do

tput sc;echo -n " "

fcolor -n --green "["; fcolor -n --red $sleep_time; fcolor -n --green "]"

tput rc;sleep 1;tput ed

done

tput reset

fi

#Print note

clear;echo;bcolor --red "Note:"

fcolor --orange `printf "%45s\n" ""|tr ' ' '='`

cat<

Description: Monitor LAN traffic

you can monitor only one !

Note: "CTRL + C" to exit !!

EOF

fcolor --orange `printf "%45s\n" ""|tr ' ' '='`

echo

while true;do

#Get variable NIC

fcolor -n --green "Select the NIC(eth0 default):"

read -t 300 NIC

NIC=`echo $NIC|tr 'A-Z' 'a-z'|tr -dc '[a-z0-9]'`

if [ -z $NIC ]; then

set $Default_nic; break

else

#To determine the existence of NIC

Judge_Nic=`ifconfig |grep '^[a-zA-Z]'|awk '{print $1}'|grep $NIC|head -n 1`

if [ -z $Judge_Nic ];then

echo The NIC \"$NIC\" doesn\'t exit or hasn\'t started!!!!|grep $NIC --color=yes

fcolor --blue "please input the NIC again!"

sleep 1

else

set $NIC; break

fi

fi

done

#return to the top of the screen;clean the screen and hide the cursor

tput cup 0 0; tput ed ;tput civis;i=1

#The body of the loop

while true;do

tput cup 0 0

#Get sum Rx adn Tx

#Rx_Sum=`ifconfig $1 | sed -n '8p' |awk '{print $3,$4}'| tr -d '[()]'`

Rx_Sum=`ifconfig $1 | grep -i tx|grep -i Rx |awk '{print $3,$4}'| tr -d '[()]'`

#Tx_Sum=`ifconfig $1 | sed -n '8p' |awk '{print $7,$8}'| tr -d '[()]'`

Tx_Sum=`ifconfig $1 | grep -i tx|grep -i Rx |awk '{print $7,$8}'| tr -d '[()]'`

#For the first time

#Rx_Before=`ifconfig $1 | sed -n '8p'| awk '{print $2}'| tr -c -d '0-9\n'`

Rx_Before=`ifconfig $1 | grep -i tx|grep -i Rx| awk '{print $2}'| tr -c -d '0-9\n'`

#Tx_Before=`ifconfig $1 | sed -n '8p'| awk '{print $6}'| tr -c -d '0-9\n'`

Tx_Before=`ifconfig $1 | grep -i tx|grep -i Rx| awk '{print $6}'| tr -c -d '0-9\n'`

sleep $Get_flow_freq

#Second times to obtain the information

#Rx_After=`ifconfig $1 | sed -n '8p'| awk '{print $2}'| tr -c -d '0-9\n'`

Rx_After=`ifconfig $1 | grep -i tx|grep -i Rx| awk '{print $2}'| tr -c -d '0-9\n'`

#Tx_After=`ifconfig $1 | sed -n '8p'| awk '{print $6}'| tr -c -d '0-9\n'`

Tx_After=`ifconfig $1 | grep -i tx|grep -i Rx| awk '{print $6}'| tr -c -d '0-9\n'`

Rx=`echo "scale=$Exact_num;($Rx_After-$Rx_Before)/$Get_flow_freq/1024"|bc`

Tx=`echo "scale=$Exact_num;($Tx_After-$Tx_Before)/$Get_flow_freq/1024"|bc`

Rx_Judge=`echo $Rx|cut -b 1`

Rx_2=`echo $Rx|awk -F "." '{print $2}'`

[ $Rx_Judge == "." ]&&Rx=0.${Rx_2}

Tx_Judge=`echo $Tx|cut -b 1`

Tx_2=`echo $Tx|awk -F "." '{print $2}'`

[ $Tx_Judge == "." ]&&Tx=0.${Tx_2}

#Application of alarm function

Alarm_Rx $Rx ;Alarm_Tx $Tx

#display the information

N=`expr $Cls_interval + 1`

if [ `expr ${i} % $N` -eq $Cls_interval ];then tput ed; fi

echo -n "*${1}:*"|grep $1 --color=yes

bcolor --green `printf "%50s\n" ""|tr ' ' '-'`

printf "%-5s%-10s%-4s\n" Rx: $Rx kB/s

printf "%-5s%-10s%-4s\n" Tx: $Tx kB/s

echo

DATE=`date "+%H:%M:%S %Y.%m.%d"`

echo "Rx: $Rx_Sum "

echo "Tx: $Tx_Sum "

echo -n -e "\t\t\t ";fcolor --pink $DATE

bcolor --green `printf "%50s\n" ""|tr ' ' '-'`

echo;echo;let i++

done

#-----------------end------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值