Counting IPv4 vs IPv6 packets on a Unifi Security Gateway

Recently (after looking at the query type stats on my PiHole I wondered how much IPv4 vs IPv6 traffic my home network was driving.

I use a UniFi Security Gateway as my router, and presumably because it’s early days for IPv6 support in UniFi, the management UI doesn’t give any breakdown on traffic types by family.

You can get this info out of netstat though, so after sshing to the USG, the following commands will give you the traffic in packets handled by IPv4 and IPv6 (no need to be root):

echo -n "ipv4: "; netstat -s | awk '/total packets/ { print $1 }'
echo -n "ipv6: "; netstat -s6 | awk '/total packets/ { print $1 }'

In my case,

ipv4: 158065944
ipv6: 849169602

So that’s 15% IPv4 and 85% IPv6, which is pretty good, and interesting that my DNS queries are about 40% A and 40% AAAA, so IPv6 traffic must be driving quite a bit more traffic per request.

(Thanks to although I rewrote the command because every time you see grep SOMETHING | awk you should remember that awk does pattern matching so you can save yourself a pipeline step.)