Skip to content

Instantly share code, notes, and snippets.

@randy3k
Created October 11, 2023 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randy3k/212ed57a315a89ff6d712fb0f67d6e23 to your computer and use it in GitHub Desktop.
Save randy3k/212ed57a315a89ff6d712fb0f67d6e23 to your computer and use it in GitHub Desktop.
Update Cloudflare ddns
import CloudFlare
import requests
TOKEN = ""
DOMAIN = ""
SUBDOMAINS = []
myip = requests.get('https://api.ipify.org').content.decode('utf8')
cf = CloudFlare.CloudFlare(token = TOKEN)
zones = cf.zones.get(params = {'name': DOMAIN})
if not zones:
raise Exception("zone not found")
zone_id = zones[0]['id']
dns_records = cf.zones.dns_records.get(zone_id)
for dns_record in dns_records:
if dns_record['name'].replace("." + DOMAIN, "") in SUBDOMAINS:
dns_record_id = dns_record['id']
cf.zones.dns_records.patch(zone_id, dns_record_id, data = {
"content": myip
})
print("Updated {} with IP {}.".format(dns_record['name'], myip))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment