Python and GeoIP library.
With GeoIP we can determine, for example, location of web site visitor's.
Let's take a look on sample code:
import pygeoip
import re
gi = pygeoip.GeoIP('/opt/geoIp/GeoLiteCity.dat')
log_file = '/var/log/nginx/access.log'
ip_regex = re.compile(r"\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
s = set()
def getLocation(ip):
"""doc string"""
rec = gi.record_by_name(ip)
print("Target ip:", ip, "located:")
print("Country code:", str(rec['country_code3']))
print("Country:", str(rec['country_name']))
print("City:", str(rec['city']))
print()
with open(log_file) as f:
for line in f:
if 'bot' in line:
continue
else:
a = re.search(ip_regex, line)
b = a.group()
s.add(b)
for ip in s:
getLocation(ip)
As result we can see following output:
Target ip: 208.101.7.44 located:
Country code: USA
Country: United States
City: Dallas