Skip to content

Display packets per second on Linux

There are many tools for network monitoring but you can do this with a simple bash script.

#!/bin/bash

time="1"     # one second
int="eth0"   # network interface

while true
	do
		txpkts_old="`cat /sys/class/net/$int/statistics/tx_packets`" # sent packets
		rxpkts_old="`cat /sys/class/net/$int/statistics/rx_packets`" # recv packets
			sleep $time
		txpkts_new="`cat /sys/class/net/$int/statistics/tx_packets`" # sent packets
                rxpkts_new="`cat /sys/class/net/$int/statistics/rx_packets`" # recv packets
		txpkts="`expr $txpkts_new - $txpkts_old`"		     # evaluate expressions for sent packets
		rxpkts="`expr $rxpkts_new - $rxpkts_old`"		     # evaluate expressions for recv packets
			echo "tx $txpkts pkts/s - rx $rxpkts pkts/ on interface $int"
	done

Output example:

tx 32 pkts/s - rx 32 pkts/ on interface eth0
tx 56 pkts/s - rx 56 pkts/ on interface eth0
tx 79 pkts/s - rx 79 pkts/ on interface eth0
tx 156 pkts/s - rx 156 pkts/ on interface eth0
tx 56 pkts/s - rx 56 pkts/ on interface eth0
tx 91 pkts/s - rx 91 pkts/ on interface eth0
tx 24 pkts/s - rx 24 pkts/ on interface eth0
tx 41 pkts/s - rx 41 pkts/ on interface eth0
tx 50 pkts/s - rx 50 pkts/ on interface eth0
tx 36 pkts/s - rx 36 pkts/ on interface eth0
tx 40 pkts/s - rx 40 pkts/ on interface eth0
tx 20 pkts/s - rx 20 pkts/ on interface eth0
Published inLinux