#!/usr/bin/python
import pcap
import sys
import time
import curses

def stats(stdscr):
    dev = sys.argv[1]

    try:
        ip6 = pcap.pcapObject()
        ip6.open_live(dev, 1600, 0, 100)
        ip6.setfilter('ip6', 0, 0)
    except:
        ip6 = pcap.pcap(dev, 1600, True, True)
        ip6.setfilter('ip6', 0)

    try:
        ip4 = pcap.pcapObject()
        ip4.open_live(dev, 1600, 0, 100)
        ip4.setfilter('ip', 0, 0)
    except:
        ip4 = pcap.pcap(dev, 1600, True, True)
        ip4.setfilter('ip', 0)


    while 1:
        total_ip6 = ip6.stats()[0]
        total_ip4 = ip4.stats()[0]
        total = total_ip6 + total_ip4
        if total != 0:
            stdscr.addstr(0, 0, """IPv6: %d (%d%%)
IPv4: %d (%d%%)
Total: %d
""" % (
                total_ip6,
                (float(total_ip6) / float(total)) * 100.0,
                total_ip4,
                (float(total_ip4) / float(total)) * 100.0,
                total
            ))
            stdscr.refresh()
        try:
            time.sleep(1)
        except KeyboardInterrupt:
            return

curses.wrapper(stats)
