Created
February 15, 2018 22:30
-
-
Save robscott/963d6de5d60905a2fbcc373e05dcf497 to your computer and use it in GitHub Desktop.
Simple script that gets all CloudFront CIDRs and prints them out as OpenVPN routes
This file contains hidden or 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 urllib.request | |
import json | |
import socket | |
import struct | |
def cidr_to_netmask(cidr): | |
network, net_bits = cidr.split('/') | |
host_bits = 32 - int(net_bits) | |
netmask = socket.inet_ntoa(struct.pack('!I', (1 << 32) - (1 << host_bits))) | |
return network, netmask | |
url = 'https://ip-ranges.amazonaws.com/ip-ranges.json' | |
req = urllib.request.Request(url) | |
r = urllib.request.urlopen(req).read() | |
ipRangeResponse = json.loads(r.decode('utf-8')) | |
for ipRange in ipRangeResponse['prefixes']: | |
if (ipRange['service'] == 'CLOUDFRONT'): | |
network, netmask = cidr_to_netmask(ipRange['ip_prefix']) | |
print("route {} {}".format(network, netmask)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment