#!/usr/bin/env python #========================================================================== # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #========================================================================== # # Eri Ramos Bastos - bastos.eri (at) gmail.com # import csv import sys try: ip = sys.argv[1] except: print "Usage: ", sys.argv[0], "ip" sys.exit(1) try: ip_number = int(ip.split(".")[0]) * (256*256*256) + int(ip.split(".")[1]) * (256*256) + int(ip.split(".")[2]) * 256 + int(ip.split(".")[3]) except: print "This doesn't looks like an IP address" sys.exit(1) try: reader = csv.reader(open("ip-to-country.csv", "rb")) # change this to fit your path except: print "Unable to open ip-to-country database" sys.exit(1) for row in reader: if int(row[0]) <= int(ip_number) and int(row[1]) >= int(ip_number): print row[4] sys.exit(0) print "Unable to find country"