Skip to content

Instantly share code, notes, and snippets.

@evindunn
Created May 26, 2022 17:16
Show Gist options
  • Save evindunn/67ac16beafe3eb54a0fe300bd0e7e7ff to your computer and use it in GitHub Desktop.
Save evindunn/67ac16beafe3eb54a0fe300bd0e7e7ff to your computer and use it in GitHub Desktop.
Generates a firewalld ipset from the current list of GCP IPs
#!/usr/bin/env python3
import xml.etree.ElementTree as ET
from urllib.request import urlopen
from json import loads as json_loads
from xml.dom import minidom
def main():
ipset = ET.Element("ipset", attrib={"type": "hash:net"})
ipset_short = ET.SubElement(ipset, "short")
ipset_short.text = "Google Cloud Platform IP ranges"
with urlopen("https://www.gstatic.com/ipranges/cloud.json") as url:
gcp_ipsets = json_loads(url.read().decode("utf-8"))
for gcp_ipset in gcp_ipsets["prefixes"]:
if "ipv4Prefix" not in gcp_ipset.keys():
continue
subnet = gcp_ipset["ipv4Prefix"]
current_entry = ET.SubElement(ipset, "entry")
current_entry.text = subnet
dom = minidom.parseString(ET.tostring(ipset, encoding="unicode"))
print(dom.toprettyxml())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment