Skip to content

Instantly share code, notes, and snippets.

@cpu
Last active December 10, 2015 04:48
Show Gist options
  • Save cpu/4383810 to your computer and use it in GitHub Desktop.
Save cpu/4383810 to your computer and use it in GitHub Desktop.
Look up one or more IPs using the free Maxmind Geolookup API (http://www.maxmind.com/en/geoip_demo). Print the results in a plain format easily processed by script/human.
#!/usr/bin/env python
#
# geoLookup.py
# https://gist.github.com/4383810
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author:
# Daniel McCarney
# https://binaryparadox.net
# daniel@binaryparadox.net
#
# Installation:
# wget -O geoLookup https://gist.github.com/raw/4383810/geoLookup.py
# chmod +x geoLookup
# ./geoLookup 207.97.227.239
#
# Dependencies:
# Python >= 2.4
# Python-Requests (http://docs.python-requests.org/en/latest/)
#
# Usage:
# geoLookup <ipAdress 1> ... <ipAddress n>
#
# Notes:
# Maxmind indicates they limit the demo to 25 lookups a day (per source IP)
# You'll probably want to mv geoLookup somewhere in your $PATH
#
# Example Output:
# $> geoLookup 72.30.38.140 74.125.226.20
# Irving, TX, United States
# Inktomi Corporation, yahoo.com, 72.30.38.140
#
# Jamaica, NY, United States
# Google, 1e100.net, 74.125.226.20
#
# $> geoLookup 72.malformed.140
# Error: INVALID_IP_ADDRESS - The value "72.malformed.140" is not a valid ip
# address
#
import os, sys
try:
import requests
except ImportError:
print("""
geoLookup module requires Python Requests to function.
Available to install with Pip or via http://docs.python-requests.org/en/latest/
""")
sys.exit(1)
API_URL = 'http://www.maxmind.com/geoip/city_isp_org/{0}?demo=1'
def lookupIP (ipAddress):
"""
Performs a lookup for a given IP address.
Returns a dict constructed with the JSON response from Maxmind.
Errors are returned as a dict with a 'code' and 'error' message keys.
"""
r = requests.get(API_URL.format(ipAddress));
if not r.json:
return {'code' : -1, 'error' : 'Unknown error'}
return r.json
def printInfo (ipInfo):
"""
Prints pertinent ip information from the given ipInfo dict.
If the dict represents an error the error code and message are printed.
Otherwise, the city, region code, and country name are printed on one line
and the ISP, domain, and ip address are printed on another.
"""
if 'error' in ipInfo:
print("Error: {code} - {error}\n".format(**ipInfo))
else:
print("{city}, {region_code}, {country_name}".format(**ipInfo))
print("{isp}, {domain}, {ip_address}\n".format(**ipInfo))
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Syntax: {0} <ipAddress 1> ... <ipAddress n>".format(sys.argv[0]))
sys.exit(1)
ips = sys.argv[1:]
res = [ lookupIP(i) for i in ips ]
[ printInfo(r) for r in res ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment