I'm interested in creating a script which prints total number of received packets and sent packets on an interface. The output should be like:

TX: number
RX: number

Can anyone help me please?

  • Did you mean something like tcpdump? – user4098326 Dec 14 '14 at 3:44
  • Can you show us what you have tried so far and any issues/errors that you faced? This should be simple. You can start with ifconfig, grep and awk. – Sree Dec 14 '14 at 7:53

You can figure out the amount of packets received and transmitted across eth0 by running the following commands:

cat /sys/class/net/eth0/statistics/rx_packets
cat /sys/class/net/eth0/statistics/tx_packets

You could then use this fact to write simple a shell script which will poll these files every second, and then calculate and output a PPS value (packets per second).


I have seen some miss matches in the stats using the /sys/class/net statistics and also same with

    ifconfig <interface>

(since it is reading stats from that files, i think). Some tools using the mentioned counters are nload and vnstat.

The correct stats i have seen is from

    ethtool -S <interface> | grep -E 'rx_packets|tx_packets|rx_bytes|tx_bytes'
  • On Ubuntu I only see total packets, not packets per second, as I've seen in an online example. I wonder if this is distribution dependent... – Nagev May 16 '19 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.