Search the AWS IP Ranges document for a specific IP.
Created
March 21, 2024 18:13
Search AWS IP list for a specific IP
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
import json | |
import ipaddress | |
from urllib.request import urlopen | |
IP_ADDRESS = "1.2.3.4" # replace me - can be IPv4 or IPv6 | |
resp = urlopen("https://ip-ranges.amazonaws.com/ip-ranges.json") | |
text = resp.read() | |
data = json.loads(text) | |
needle = ipaddress.ip_address(IP_ADDRESS) | |
is_ipv6 = isinstance(needle, ipaddress.IPv6Address) | |
haystack = data["ipv6_prefixes"] if is_ipv6 else data["prefixes"] | |
for prefix in haystack: | |
cidr = prefix["ipv6_prefix"] if is_ipv6 else prefix["ip_prefix"] | |
network = ipaddress.ip_network(cidr) | |
if needle in network: | |
print(prefix) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment