Advertisement
opexxx

geolookup.py

Feb 24th, 2014
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Simple GeoIP DB lookup
  3.  
  4. import argparse
  5. import sys
  6. import os
  7. import geoip2.database
  8.  
  9. def checkip():
  10.     """Check the IP address against our DB"""
  11.     print "Checking IP's"
  12.  
  13.  
  14. def checkdb():
  15.     """Check that our DB files exist"""
  16.     print "Checking databases"
  17.  
  18.     fullpath = os.getenv("HOME")
  19.     try:
  20.         dbfile = open(fullpath + '/.glcountry.mmdb', 'r')
  21.     except:
  22.         sys.exit("** ERROR ** \n> Database file not found. Please check ~/.glcountry.mmdb")
  23.  
  24.     reader = geoip2.database.Reader(dbfile)
  25.     response = reader.city('128.101.101.101')
  26.     isocode = response.country.iso_code
  27.     countryname = response.country.name
  28.     cityname = response.city.name
  29.  
  30.     print "isocode " + isocode
  31.     print "countryname " + countryname
  32.     print "city " + cityname
  33.  
  34.     #checkip()
  35.  
  36.  
  37. def __main__():
  38.  
  39.     parser = argparse.ArgumentParser(description='basic GeoIP  system', usage='%(prog)s -i IP address')
  40.     parser.add_argument('--ip', '-i', dest='ip', help='IP address to lookup')
  41.     parser.add_argument('--version', '-v', action='version', version='%(prog)s 0.1')
  42.     args = parser.parse_args()
  43.     aip = args.ip
  44.  
  45.     if not args.ip:
  46.         sys.exit(parser.print_help())
  47.  
  48.     checkdb()
  49.  
  50.  
  51. if __name__ == '__main__':
  52.     __main__()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement