Skip to content

Instantly share code, notes, and snippets.

@gurchik
Created March 21, 2024 18:13
Show Gist options
  • Save gurchik/252038ab05d842e00b84a1e42014c893 to your computer and use it in GitHub Desktop.
Save gurchik/252038ab05d842e00b84a1e42014c893 to your computer and use it in GitHub Desktop.
Search AWS IP list for a specific IP
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