Skip to content

Instantly share code, notes, and snippets.

@zerolagtime
Created September 18, 2025 15:48
Show Gist options
  • Select an option

  • Save zerolagtime/065df99c86678614fe9bf124ddc081b4 to your computer and use it in GitHub Desktop.

Select an option

Save zerolagtime/065df99c86678614fe9bf124ddc081b4 to your computer and use it in GitHub Desktop.
Locate an AWS IP address in their publicly listed list to isolate which service and region hosts the address
#!/usr/bin/env python3
# Given an IP address, find out if it's from inside an AWS controlled subnet
# and any public information about where
import json
from ipaddress import ip_address, ip_network
import sys
import urllib.request
def fetch_and_parse_ip_ranges():
"""Fetches and parses the ip-ranges.json file from AWS."""
try:
url = "https://ip-ranges.amazonaws.com/ip-ranges.json"
response = urllib.request.urlopen(url)
aws_data = json.loads(response.read())
return aws_data
except Exception as e:
print(f"Failed to fetch {url}: {e}")
return None
def check_ip_in_ranges(ip_to_check, aws_data):
"""Checks if an IP address is present in any of the networks defined in the ip-ranges.json data."""
try:
# Convert input to IPv4Address or IPv6Address object
target_ip = ip_address(ip_to_check)
for prefix in aws_data['prefixes']:
network = ip_network(prefix['ip_prefix'], strict=False)
# Check if the target IP is part of this network
if target_ip in network:
print(f"{target_ip} is in CIDR block {prefix['ip_prefix']}, service {prefix['service']} in the {prefix['region']} region")
return True
print(f"{target_ip} not found in any range.")
return False
except ValueError:
print(f"Invalid IP address: {ip_to_check}")
except Exception as e:
print(f"An error occurred: {e}")
def main():
if len(sys.argv) != 2:
print("Usage: python script_name.py <IP_ADDRESS>")
sys.exit(1)
ip_to_check = sys.argv[1]
aws_data = fetch_and_parse_ip_ranges()
if aws_data:
check_ip_in_ranges(ip_to_check, aws_data)
if __name__ == "__main__":
main()
@zerolagtime
Copy link
Author

Sample usage and output:
Command: python3 locate_aws_ip.py 3.24.4.5
Output: 3.24.4.5 is in CIDR block 3.24.0.0/14, service AMAZON in the ap-southeast-2 region

Command: python3 locate_aws_ip.py 11.1.2.3
Output: 11.1.2.3 not found in any range.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment