Alex Rivera | Logout

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

Asked 2008-12-08T13:38:53.937
12

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.
Edit
Report

2 Answers

12
cat /proc/net/dev

Should contain counters, statistics, and information.

answered 2009-02-04T13:24:32.903
7

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.

answered 2008-12-08T13:57:43.523

Your Answer