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:

interface
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
up vote 7 down vote accepted

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'

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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