#!/bin/bash
#
# Calculate the percentage of IP traffic that is IPv6 in a IPv4/
# IPv6 dual-stack server environment. Works on plain-text or
# compressed log formats that contain IP addresses.

if [ -z "$1" ]; then
  echo "Usage: $0 <log-path>"
  exit 1
fi

# Count number of lines matching each each protocol version
# using regexes for matching IPv4 and IPv6 addresses.
IPV4COUNT=$(zcat -f $@ | egrep -e "([0-9]{1,3}\.){3}[0-9]{1,3}"\
| wc -l)
IPV6COUNT=$(zcat -f $@ | egrep -e "(([0-9a-fA-F]{1,4}:){7,7}[0-\
9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6\
}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){\
1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA\
-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2\
}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4})\
{1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0\
,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-\
4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9])\
{0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0\
-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\
)" | wc -l)

# Find IPv6 percentage out of total number of addrsses
IPV6SHARE=$(echo  "scale=3; 100 * ${IPV6COUNT} / (${IPV6COUNT} \
+${IPV4COUNT})  " | bc)

echo "${IPV6SHARE}% of scanned addresses are IPv6"