2020-08-04 03:11:35 +02:00
|
|
|
#!/usr/bin/python3.8
|
|
|
|
|
2020-11-24 23:07:38 +01:00
|
|
|
import argparse
|
2020-08-04 03:11:35 +02:00
|
|
|
import sys
|
|
|
|
|
|
|
|
try:
|
|
|
|
import geoip2.database
|
2020-11-24 23:07:38 +01:00
|
|
|
import geoip2.errors
|
2020-08-04 03:11:35 +02:00
|
|
|
except ImportError:
|
|
|
|
print(
|
2020-11-24 23:07:38 +01:00
|
|
|
"Required modules geoip2.database and geoip2.errors not found. On Gentoo Linux, please install dev-python/geoip2 from the 'fritteli' overlay.",
|
2020-08-04 03:11:35 +02:00
|
|
|
file=sys.stderr)
|
|
|
|
exit(1)
|
|
|
|
|
2020-11-24 23:07:38 +01:00
|
|
|
try:
|
|
|
|
import maxminddb.errors
|
|
|
|
except ImportError:
|
|
|
|
print(
|
|
|
|
"Required module maxminddb.errors not found. On Gentoo Linux, please install dev-python/maxminddb from the 'fritteli' overlay.",
|
|
|
|
file=sys.stderr)
|
|
|
|
exit(1)
|
2020-08-04 03:11:35 +02:00
|
|
|
|
|
|
|
class LookupException(Exception):
|
|
|
|
"""
|
|
|
|
General Exception class that is raised if anything goes wrong.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def get_county_code(ipaddress, dbfile):
|
|
|
|
"""
|
|
|
|
Determine the country code that the given ipaddress comes from.
|
|
|
|
:param ipaddress: The IP address to look up
|
|
|
|
:param dbfile: The path to the GeoIP2/GeoLite2 database file (Country or City database)
|
|
|
|
:return: The ISO country code (2 letters)
|
|
|
|
"""
|
|
|
|
if not ipaddress:
|
|
|
|
raise LookupException("No address given")
|
|
|
|
if not dbfile:
|
|
|
|
raise LookupException("No db file given")
|
|
|
|
|
|
|
|
reader = None
|
|
|
|
try:
|
|
|
|
reader = geoip2.database.Reader(dbfile)
|
|
|
|
dbtype = reader.metadata().database_type
|
|
|
|
country = None
|
|
|
|
if dbtype == 'GeoLite2-City' or dbtype == 'GeoIP2-City':
|
|
|
|
country = reader.city(ipaddress).country
|
|
|
|
elif dbfile == 'GeoLite2-Country' or dbtype == 'GeoIP2-Country':
|
|
|
|
country = reader.country(ipaddress).country
|
|
|
|
# ASN is not supported
|
|
|
|
# elif dbfile == 'GeoLite2-ASN' or dbtype == 'GeoIP2-ASN':
|
|
|
|
|
|
|
|
if not country:
|
|
|
|
raise LookupException("Unsupported DB type: " + dbtype)
|
|
|
|
|
|
|
|
return country.iso_code
|
2020-11-24 23:07:38 +01:00
|
|
|
except FileNotFoundError as e:
|
|
|
|
raise LookupException(e.args)
|
|
|
|
except maxminddb.errors.InvalidDatabaseError as e:
|
|
|
|
raise LookupException(e.args)
|
|
|
|
except geoip2.errors.AddressNotFoundError as e:
|
|
|
|
raise LookupException(e.args)
|
|
|
|
except ValueError as e:
|
|
|
|
raise LookupException(e.args)
|
2020-08-04 03:11:35 +02:00
|
|
|
finally:
|
|
|
|
if reader:
|
|
|
|
reader.close()
|
|
|
|
|
|
|
|
|
|
|
|
def parse_command_line(argv):
|
|
|
|
"""
|
|
|
|
Parse the command line. First, the database file must be specified ("-f /path/to/db/file.mmdb"), then the IP address
|
|
|
|
to look up
|
|
|
|
:param argv:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
dbfile = None
|
2020-11-24 23:07:38 +01:00
|
|
|
parser = argparse.ArgumentParser(description='Get the country code from an IP address')
|
|
|
|
parser.add_argument('-f', dest='dbfile', required=True, help="Path to the GeoIP2 database file")
|
|
|
|
parser.add_argument('address', help="The IP address to check")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
return args.dbfile, args.address
|
2020-08-04 03:11:35 +02:00
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
"""
|
|
|
|
Read the database file and the IP address from the command line and print the corresponding ISO country code on
|
|
|
|
stdout.
|
|
|
|
:param argv: Format: "-f /path/to/database.mmdb ip.v4.add.ress"
|
|
|
|
:return:
|
|
|
|
"""
|
2020-11-24 23:07:38 +01:00
|
|
|
try:
|
2020-11-24 22:04:38 +01:00
|
|
|
(dbfile, ipaddress) = parse_command_line(argv)
|
|
|
|
code = get_county_code(ipaddress, dbfile)
|
|
|
|
print(code)
|
|
|
|
except LookupException as e:
|
2020-11-24 23:07:38 +01:00
|
|
|
print(e.args, file=sys.stderr)
|
2020-11-24 22:04:38 +01:00
|
|
|
print("Unknown")
|
2020-08-04 03:11:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
main(sys.argv[1:])
|
|
|
|
except BaseException as e:
|
2020-11-24 23:07:38 +01:00
|
|
|
print("Unknown")
|
2020-08-04 03:11:35 +02:00
|
|
|
raise e
|