Cayle Spandon Cayle Spandon - 2 months ago 5x
Linux Question

Linux: retrieve per-interface sent/received packet counters (ethernet, ipv4, ipv6)

On Linux, how can I (programmatically) retrieve the following counters on a per-interface basis:

  • Sent/received ethernet frames,

  • Sent/received IPv4 packets,

  • Sent/received IPv6 packets.


You should be able to do this using iptables rules and packet counters, e.g.

# input and output must be accounted for separately
# ipv4, eth0
iptables -I INPUT -i eth0
iptables -I OUTPUT -o eth0
# ipv6, eth0
ip6tables -I INPUT -i eth0
ip6tables -I OUTPUT -o eth0

And to view the stats, parse the output of these:

iptables -L -vxn
ip6tables -L -vxn

You should also look up the -Z flag for when you want to reset the counters.