Linux shell counting traffic by network interface

 Linux shell Counting traffic by network interface

#!/bin/sh

RXpre=""
TXpre=""
while [ "1" ]
do
	eth=$1
	if [ "${RXpre}" == "" -o "${TXpre}" == "" ]; then
		RXpre=$(cat /proc/net/dev | grep -w $eth | tr : " " | awk '{print $2}')
		TXpre=$(cat /proc/net/dev | grep -w $eth | tr : " " | awk '{print $10}')
	fi
	
	sleep 1
	
	RXnext=$(cat /proc/net/dev | grep -w $eth | tr : " " | awk '{print $2}')
	TXnext=$(cat /proc/net/dev | grep -w $eth | tr : " " | awk '{print $10}')
	
	clear
	echo  -e  "\t RX `date +%k:%M:%S` TX"
	
	RX=$((${RXnext}-${RXpre}))
	TX=$((${TXnext}-${TXpre}))

	if [[ $RX -lt 1024 ]];then
		RX="${RX}B/s"
	elif [[ $RX -gt 1048576 ]];then
		RX=$(echo $RX | awk '{print $1/1048576 "MB/s"}')
	else
		RX=$(echo $RX | awk '{print $1/1024 "KB/s"}')
	fi

	if [[ $TX -lt 1024 ]];then
		TX="${TX}B/s"
	elif [[ $TX -gt 1048576 ]];then
		TX=$(echo $TX | awk '{print $1/1048576 "MB/s"}')
	else
		TX=$(echo $TX | awk '{print $1/1024 "KB/s"}')
	fi

	RXpre=$RXnext
	TXpre=$TXnext

	echo -e "$eth \t $RX   $TX "
done
Execution: sh flow.sh eth0

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example