Skip to content

Instantly share code, notes, and snippets.

@complexsplit
Last active July 16, 2019 16:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save complexsplit/0a099c37df8ca6c7c855cc7bcdf127b7 to your computer and use it in GitHub Desktop.
Save complexsplit/0a099c37df8ca6c7c855cc7bcdf127b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import netaddr
import json
import DNS # dns-python in ubuntu http://sourceforge.net/projects/pydns/
import urllib
awstotal = 0
gcptotal = 0
AWSURL = 'https://ip-ranges.amazonaws.com/ip-ranges.json'
GCPROOTDNS = '_cloud-netblocks.googleusercontent.com'
# calculate aws ec2 ip range
response = urllib.urlopen(AWSURL)
awsdata = json.loads(response.read())
for entry in awsdata['prefixes']:
if entry['service'] == 'EC2':
ip = netaddr.IPNetwork(entry['ip_prefix'])
awstotal += ip.size
# calculate gcp gce ip range
# TXT record looks like:
# dig +short _cloud-netblocks.googleusercontent.com TXT
# "v=spf1 include:_cloud-netblocks1.googleusercontent.com include:_cloud-netblocks2.googleusercontent.com
# include:_cloud-netblocks3.googleusercontent.com include:_cloud-netblocks4.googleusercontent.com
# include:_cloud-netblocks5.googleusercontent.com ?all"
maindnsrecord = DNS.dnslookup(GCPROOTDNS, "TXT")[0][0]
includes = [x for x in maindnsrecord.split() if x.startswith("include:")]
for toprecord in includes:
subrecords = (DNS.dnslookup(toprecord.split(":")[1], "TXT"))[0][0]
subrecords = subrecords.split(" ")
subrecords = [x for x in subrecords if x.startswith("ip")]
for ipblocks in subrecords:
if ipblocks.startswith("ip4"):
ip4range = netaddr.IPNetwork(ipblocks[4:])
gcptotal += ip4range.size
if ipblocks.startswith("ip6"):
ip6range = netaddr.IPNetwork(ipblocks[4:])
gcptotal += ip6range.size
print "AWS total: " + str(awstotal)
print "GCP total: " + str(gcptotal)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment