-
Notifications
You must be signed in to change notification settings - Fork 3
/
geolocip.py
executable file
·54 lines (45 loc) · 1.43 KB
/
geolocip.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
import argparse
import fileinput
import geoip2.database
import sys
maxmind_cc_db = '/etc/GeoLite2-Country.mmdb'
maxmind_city_db = '/etc/GeoLite2-City.mmdb'
parser = argparse.ArgumentParser()
parser.add_argument('-i') # IP address argument, otherwise read stdin
args = parser.parse_args()
def geoloc(ipaddr):
with geoip2.database.Reader(maxmind_cc_db) as reader:
try:
response = reader.country(ipaddr)
cc = response.country.iso_code
except:
cc = '-'
with geoip2.database.Reader(maxmind_city_db) as reader:
try:
response = reader.city(ipaddr)
city = response.city.name
lat = response.location.latitude
long = response.location.longitude
except:
city = '-'
lat = '-'
long = '-'
return lat, long, cc, city
def writeinfo(ipaddr, lat, long, cc, city):
if ':' in ipaddr:
sys.stdout.write("%-37s | %s | %s | %s | %s\n"
% (ipaddr,lat,long,cc,city))
else:
sys.stdout.write("%-15s | %s | %s | %s | %s\n"
% (ipaddr,lat,long,cc,city))
return
if args.i:
lat, long, cc, city = geoloc(args.i)
writeinfo(args.i, lat, long, cc, city)
else:
for line in fileinput.input():
ipaddr = line.rstrip()
lat, long, cc, city = geoloc(ipaddr)
writeinfo(ipaddr, lat, long, cc, city)
sys.exit(0)