diff --git a/GeolocationLookup/GeolocationLookup.py b/GeolocationLookup/GeolocationLookup.py new file mode 100644 index 0000000..e77cf13 --- /dev/null +++ b/GeolocationLookup/GeolocationLookup.py @@ -0,0 +1,54 @@ +import sys +import json +import argparse +import requests + +# Define functions + +def create_parser(): + parser = argparse.ArgumentParser() + parser.add_argument('-k', '--apikey', metavar='IP2Location.io API Key.') + parser.add_argument('-p', '--ip', metavar='Specify an IP address.') + + return parser + +# Define variables +keyless = False +self_ip_query = False +parameters = {} + +if len(sys.argv) > 1: + # check if API key is provided or not + parser = create_parser() + args = parser.parse_args(sys.argv[1:]) + if args.apikey is None: + keyless = True + else: + parameters['key'] = args.apikey + if args.ip is None: + self_ip_query = True + else: + parameters['ip'] = args.ip +else: + # Assuming using Keyless API + keyless = True + +if keyless: + print(f"Calling keyless API now...") + if len(parameters) > 0: + r = requests.get('https://api.ip2location.io/', params=parameters) + else: + r = requests.get('https://api.ip2location.io/') +else: + print(f"Calling API now...") + if len(parameters) > 0: + r = requests.get('https://api.ip2location.io/', params=parameters) + else: + r = requests.get('https://api.ip2location.io/') + +if r.json() == None: + print(f"Unexpected error, please try again later.") +elif 'error' in r.json(): + print(f"IP2Location.io API error: {r.json()['error']['error_message']}") +else: + print(r.json()) diff --git a/GeolocationLookup/README.md b/GeolocationLookup/README.md new file mode 100644 index 0000000..a5f5dcb --- /dev/null +++ b/GeolocationLookup/README.md @@ -0,0 +1,43 @@ +# Geolocation Lookup + +This script make use of [IP2Location.io API](https://www.ip2location.io/) to query enrich geolocation information for an IP address. The script supports both normal API call and keyless API call. The keyless API had a limitation of 500 queries per day. Once this limit is reached, any additional API calls will fail until the time period resets at 00:00 UTC daily. + +## Parameters + +|Parameter| Description | +|--|--| +|-k, --apikey | IP2Location.io API Key. You can sign up a free one from https://www.ip2location.io/sign-up. If not provided, the script will use keyless API. | +|-p, --ip | IP address to be query for. If not provided, the source IP address will be use to query. | + + +## Usage + +Before running the script, make sure install the required libraries first: + +``` + pip install -r requirements.txt +``` + +To perform normal query with the API Key, you can use `python GeolocationLookup.py -k -p `. Replaced the relevant values before run the command. + +For keyless API call, you can run`python GeolocationLookup.py` or `python GeolocationLookup.py -p `. + +For a successful call, you shall getting a JSON result printed out. For instance, you will see the following output for the IP address 8.8.8.8 for keyless API: + +```json +{ + "ip": "8.8.8.8", + "country_code": "US", + "country_name": "United States of America", + "region_name": "California", + "city_name": "Mountain View", + "latitude": 37.38605, + "longitude": -122.08385, + "zip_code": "94035", + "time_zone": "-07:00", + "asn": "15169", + "as": "Google LLC", + "is_proxy": false, + "message": "Limit to 500 queries per day. Sign up for a Free plan at https://www.ip2location.io to get 30K queries per month." +} +``` diff --git a/GeolocationLookup/requirements.txt b/GeolocationLookup/requirements.txt new file mode 100644 index 0000000..663bd1f --- /dev/null +++ b/GeolocationLookup/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file