Last active
November 12, 2019 00:10
-
-
Save 0xbharath/33894ca87bee30ac3bdc87a17add94d3 to your computer and use it in GitHub Desktop.
A quick script to determine AWS Region from IP Address
This file contains 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
from ipaddress import ip_network, ip_address | |
import json | |
import requests | |
import sys | |
def find_aws_region(): | |
ip_json = requests.get('https://ip-ranges.amazonaws.com/ip-ranges.json') | |
#ip_json = json.load(open('ip-ranges.json')) | |
ip_json = ip_json.json() | |
prefixes = ip_json['prefixes'] | |
my_ip = ip_address(sys.argv[1]) | |
region = 'Unknown' | |
for prefix in prefixes: | |
if my_ip in ip_network(prefix['ip_prefix']): | |
region = prefix['region'] | |
print(region) | |
break | |
return region | |
if __name__ == "__main__": | |
find_aws_region() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment