Skip to content

Instantly share code, notes, and snippets.

@carlsverre
Created December 9, 2020 00:02
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 carlsverre/39d21601f2d79a7df48317b77b81518c to your computer and use it in GitHub Desktop.
Save carlsverre/39d21601f2d79a7df48317b77b81518c to your computer and use it in GitHub Desktop.
Output all the IP addresses (not CIDR's) for each of the major cloud providers (gcp, azure, aws)
  1. Download process.py
  2. Run
~/csp $ python3 process.py
usage: process.py aws|azure|gcp

~/csp $ python3 process.py aws | head -n 10
3.5.140.0
3.5.140.1
3.5.140.2
3.5.140.3
3.5.140.4
3.5.140.5
3.5.140.6
3.5.140.7
3.5.140.8
3.5.140.9
import ipaddress
import json
from urllib.request import urlopen, Request
import re
import sys
def fetchjson(url):
return json.load(urlopen(url))
def expand(cidr):
net = ipaddress.ip_network(cidr)
return map(lambda n: str(n), net)
def aws():
d = fetchjson('https://ip-ranges.amazonaws.com/ip-ranges.json')
for prefix in d["prefixes"]:
for ip in expand(prefix.get("ip_prefix", prefix.get("ipv6_prefix"))):
yield ip
def gcp():
d = fetchjson('https://www.gstatic.com/ipranges/cloud.json')
for prefix in d["prefixes"]:
for ip in expand(prefix.get("ipv4Prefix", prefix.get("ipv6Prefix"))):
yield ip
def azure():
# find download link lol
index = urlopen(Request('https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519', headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
})).read().decode('utf-8')
reDownload = re.compile(r'https://download.microsoft.com/download[^"]+')
direct_url = reDownload.search(index)[0]
d = fetchjson(direct_url)
for o in d["values"]:
for prefix in o["properties"]["addressPrefixes"]:
for ip in expand(prefix):
yield ip
CSP = {
"aws": aws,
"azure": azure,
"gcp": gcp,
}
if __name__ == "__main__":
if len(sys.argv) != 2 or sys.argv[1] not in CSP:
print(f"usage: {sys.argv[0]} {'|'.join(CSP.keys())}")
sys.exit(1)
for ip in CSP[sys.argv[1]]():
sys.stdout.write(ip)
sys.stdout.write("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment