Created
January 10, 2017 10:07
-
-
Save johnstcn/b45a86d42358240bb7b82e8f4ea3236f to your computer and use it in GitHub Desktop.
Print a frequency distribution of countries for a list of IP addresses using a local GeoIP database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import json | |
import re | |
import sys | |
import GeoIP | |
from collections import Counter | |
GEOCODER = GeoIP.open("/usr/local/share/GeoIP/GeoIP.dat", GeoIP.GEOIP_STANDARD) | |
def perform(args): | |
country_map = Counter() | |
for line in args.input_file: | |
if re.search(r'^\s*[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s*$', line): | |
ipaddr = line.strip() | |
country = GEOCODER.country_code_by_addr(ipaddr) | |
if args.only: | |
if country in args.only: | |
country_map[country] += 1 | |
else: | |
country_map['*'] += 1 | |
else: | |
country_map[country] += 1 | |
for key, value in country_map.iteritems(): | |
args.output_file.write('%s\t%s\n' % (key, value)) | |
def main(): | |
ap = argparse.ArgumentParser() | |
ap.add_argument('input_file', nargs='?', type=argparse.FileType('r'), default=sys.stdin) | |
ap.add_argument('output_file', nargs='?', type=argparse.FileType('w'), default=sys.stdout) | |
ap.add_argument('--only', type=str, nargs='+', default=[]) | |
args = ap.parse_args() | |
perform(args) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment