Created
November 2, 2021 16:16
-
-
Save foooomio/5df5a213c586d51c7dd50d8cdb3794fa 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
#!/usr/bin/env python3 | |
# https://stackoverflow.com/questions/45441865/determine-aws-region-from-ip-address | |
from ipaddress import ip_network, ip_address | |
import requests | |
import sys | |
def find_aws_region(ip): | |
url = 'https://ip-ranges.amazonaws.com/ip-ranges.json' | |
ip_json = requests.get(url).json() | |
prefixes = ip_json['prefixes'] | |
my_ip = ip_address(ip) | |
for prefix in prefixes: | |
if my_ip in ip_network(prefix['ip_prefix']): | |
return prefix['region'] | |
return 'Unknown' | |
if __name__ == '__main__': | |
print(find_aws_region(sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment