Skip to content

Instantly share code, notes, and snippets.

@foooomio
Created November 2, 2021 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foooomio/5df5a213c586d51c7dd50d8cdb3794fa to your computer and use it in GitHub Desktop.
Save foooomio/5df5a213c586d51c7dd50d8cdb3794fa to your computer and use it in GitHub Desktop.
#!/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