Skip to content

Instantly share code, notes, and snippets.

@GauntletWizard
Created March 1, 2017 21:18
Show Gist options
  • Save GauntletWizard/c759558790281aa57c7765f1f3d4527e to your computer and use it in GitHub Desktop.
Save GauntletWizard/c759558790281aa57c7765f1f3d4527e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# gceips.py - A script to print the cidr ranges of all of GCE.
# https://cloud.google.com/compute/docs/faq#where_can_i_find_short_product_name_ip_ranges
import re
# pip install dnspython
import dns.resolver
from dns.rdatatype import TXT
includesmatch = re.compile(r'include:([\w\.-]*)')
includes = []
includesquery = dns.resolver.query('_cloud-netblocks.googleusercontent.com', TXT)
for record in includesquery.rrset.items:
includes.extend(includesmatch.findall(record.__str__()))
# Write me a regex to match an IP address.
cidrs = []
ipmatch = re.compile(r'ip4:([\d\./]*)')
for group in includes:
includequery = dns.resolver.query(group, TXT)
for record in includequery.rrset.items:
cidrs.extend(ipmatch.findall(record.__str__()))
for cidr in cidrs:
print(' "%s",' % cidr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment