Created
March 1, 2017 21:18
-
-
Save GauntletWizard/c759558790281aa57c7765f1f3d4527e to your computer and use it in GitHub Desktop.
This file contains 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
#!/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