Last active
December 15, 2015 07:59
-
-
Save tinnet/5227605 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# download GeoIPCity.dat from http://dev.maxmind.com/geoip/geolite | |
# pip install pygeoip | |
# get ip list eg. from sshguard logs: | |
# zgrep Blocking /var/log/auth.log* | cut -d ' ' -f 7 | cut -d ':' -f 1 | sort -u > ips.txt | |
# python geoiplookup.py ips.txt (or directly cat) | |
import fileinput | |
import sys | |
import pygeoip | |
gi = pygeoip.GeoIP('./GeoIPCity.dat') | |
def lookup(ip): | |
record = gi.record_by_addr(ip) | |
if 'city' in record: | |
return record['country_name'], record['city'] | |
return (record['country_name'], ) | |
if __name__ == '__main__': | |
for line in fileinput.input(): | |
print lookup(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example output