Geolocation and Python

There are a lot of different services where you can ask the Geolocation for IP and/or Domains. With python it is very easy to make requests on that APIs. As security tester you can use it on the discovery term.

Example:

#!/usr/bin/env python
#  -*- coding: utf-8 -*-

import requests
import sys


class GeoLocation(object):

    def __init__(self):
        self.url = 'http://ip-api.com/json/'

    def parse_args(self):
        self.url = self.url + sys.argv[1]

    def get_args(self):
        usr_input = raw_input("Insert ip/url: ")

        if not usr_input:
            sys.exit(1)
        else:
            self.url = self.url + usr_input

    def show_results(self):
        response = requests.get(self.url)
        output = response.json()

        for key, val in output.items():
            if val:
                print key, "=>", val


if __name__ == '__main__':
    RUN = GeoLocation()
    if len(sys.argv) < 2:
        RUN.get_args()
    else:
        RUN.parse_args()
    RUN.show_results()