123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- # Requirements:
- # Python 2.7
- import sys
- import GeoIP
- gi = GeoIP.open("/usr/share/GeoIP/GeoIP.dat", GeoIP.GEOIP_STANDARD)
- filename_ips = sys.argv[1]
- country_cnt = {}
- ips_all = set()
- fd_read = open(filename_ips, "r")
- cnt = 0
- cnt_unique = 0
- for ip in fd_read:
- cnt += 1
- ip = ip.strip()
- if ip in ips_all:
- continue
- cnt_unique += 1
- ips_all.add(ip)
- match = gi.country_name_by_addr(ip)
- if match is None:
- print("could not resolve: %s" % ip)
- continue
- country = match
- try:
- country_cnt[country] += 1
- except:
- country_cnt[country] = 1
- fd_read.close()
- print("Total: %d" % cnt)
- print("Unique: %d" % cnt_unique)
- print("Resolved: %d" % sum([count for country,count in country_cnt.items()]))
- print("")
- country_cnt_sorted = sorted(country_cnt.items(), key=lambda v: v[1])
- for country, cnt in country_cnt_sorted:
- print("%s: %d" % (country, cnt))
|