Network traffic on NICs in mbps without sar, iperf, etc...

RX1=`cat /sys/class/net/${INTERFACE}/statistics/rx_bytes` (see script below)
Need output in mbps (bits) # ./bytes-second.sh eth0 eth0 interface maximum Speed: 1000Mb/s RX:12883212 TX:17402002 B/s | RX:98 TX:132 Mb/s RX:12371647 TX:17830111 B/s | RX:94 TX:136 Mb/s RX:12502750 TX:17860915 B/s | RX:95 TX:136 Mb/s
Sample Output
while true do
	RX1=`cat /sys/class/net/${INTERFACE}/statistics/rx_bytes`
	TX1=`cat /sys/class/net/${INTERFACE}/statistics/tx_bytes`
	DOWN=$(($RX1-$RX2))
	UP=$(($TX1-$TX2))
	DOWN_Bits=$(($DOWN * 8 ))
	UP_Bits=$(($UP * 8 ))
	DOWNmbps=$(( $DOWN_Bits >> 20 ))
	UPmbps=$(($UP_Bits >> 20 ))
	echo -e "RX:${DOWN}\tTX:${UP} B/s | RX:${DOWNmbps}\tTX:${UPmbps} Mb/s"
	RX2=$RX1; TX2=$TX1
sleep 1; done

1
By: jp
2012-01-23 18:22:44

What Others Think

Would like to know if someone else has a better way. I'm not able to install any other tools on these servers, like sar, iperf... etc... A crude measurement like above is ok.
jp · 514 weeks ago
the rx_bytes is the quantity of bytes received on the card (can be seen with ifconfig). It is bytes, not bytes/s
lefada · 513 weeks and 4 days ago
function humanValue() { h=( '' K M G T P ) i=1; v=$(( $1 * 8 )) while [ $v -gt $(( 1 echo -n "$(( $v >> 10 * --i )) ${h[i]}b/s"; } ifaces=$(ip addr | grep -E "^[0-9]:" | cut -d" " -f2 | tr -d \:) declare -A RX2 TX2; while sleep 1; do date for INTERFACE in $ifaces; do RX1=$(cat /sys/class/net/${INTERFACE}/statistics/rx_bytes) TX1=$(cat /sys/class/net/${INTERFACE}/statistics/tx_bytes) DOWN=$(( RX1 - RX2[$INTERFACE] )) UP=$(( TX1 - TX2[$INTERFACE] )) RX2[$INTERFACE]=$RX1; TX2[$INTERFACE]=$TX1 echo -e "[ $INTERFACE:\tRX: $(humanValue $DOWN)\t|\tTX: $(humanValue $UP) ]" done; done;
glaudiston · 182 weeks and 6 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: