forked from andymckay/actions-ips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ips.py
62 lines (51 loc) · 1.75 KB
/
ips.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import re
import requests
import subprocess
import sys
import time
url = "https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519"
lookup_regex = re.compile(r"https://download\.microsoft\.com/download/(.*?)\.json")
def cidrs():
page = requests.get(url)
page.raise_for_status()
# You should never do a regex search on HTML, but I think in this case we'll be ok. Grep the page to find the download JSON file.
searched = lookup_regex.search(page.content.decode("utf8"))
# Couldn't find the regex on the page.
assert searched
url = searched.group()
# Now go get the JSON data.
response = requests.get(url)
response.raise_for_status()
data = response.json()
zones = [
"AzureCloud.eastus2", # EUS2
"AzureCloud.eastus", # EUS
"AzureCloud.westus", # WUS
"AzureCloud.westus2", # WUS2
"AzureCloud.centralus", # CUS
"AzureCloud.southcentralus", # SCUS
]
ips = set()
# Add in the hard coded Mac IPs in CIDR format to join the downloaded IPs.
ips.add("199.7.166.9/32")
ips.add("199.7.166.10/31")
ips.add("199.19.85.25/32")
ips.add("199.19.85.26/31")
ips.add("208.83.5.226/31")
ips.add("208.83.5.228/31")
ips.add("208.83.5.230/32")
for line in data["values"]:
if line["name"] in zones:
for address in line["properties"]["addressPrefixes"]:
ips.add(address)
def sort(address):
bits = address.split(".")
# Lets split the last on the / to make it easy to order.
last = bits[-1].split("/")
return list(map(int, bits[:3] + last))
ips = sorted(ips, key=sort)
return ips
if __name__=='__main__':
results = cidrs()
for result in results:
print(result)