-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetips.py
31 lines (26 loc) · 1017 Bytes
/
getips.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Standard Python Libraries
import ipaddress
import json
from typing import Iterator, Union
import urllib.request
def output_cidrs(
filename: str, cidrs: Iterator[Union[ipaddress.IPv4Network, ipaddress.IPv6Network]]
):
"""Output the given CIDRs to the given file."""
with open(filename, "w", encoding="utf-8") as out:
for cidr in cidrs:
out.write(f"{cidr}\n")
with urllib.request.urlopen("https://ip-ranges.amazonaws.com/ip-ranges.json") as url:
data = json.loads(url.read().decode())
# Print all AWS IPs
all_aws_ips = ipaddress.collapse_addresses(
ipaddress.ip_network(ip_prefix["ip_prefix"]) for ip_prefix in data["prefixes"]
)
output_cidrs("awsips.txt", all_aws_ips)
# Print all Cloudfront IPs
cloudfront_ips = ipaddress.collapse_addresses(
ipaddress.ip_network(ip_prefix["ip_prefix"])
for ip_prefix in data["prefixes"]
if "CLOUDFRONT" in ip_prefix["service"]
)
output_cidrs("cloudfront.txt", cloudfront_ips)