Created
September 12, 2024 17:46
-
-
Save randy3k/161d52928f074f879cab310cea9699a5 to your computer and use it in GitHub Desktop.
Cloudflare dns update
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
import CloudFlare | |
import requests | |
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: | |
dns_record_name = dns_record['name'].replace(".<DOMAIN>", "") | |
if dns_record_name in ["<HOST>"]: | |
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 + ".routeme.xyz", myip)) |
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
import cloudflare | |
import requests | |
myip = requests.get('https://api.ipify.org').content.decode('utf8') | |
cf = cloudflare.Cloudflare( | |
api_token='<TOKEN>' | |
) | |
zones = cf.zones.list(name='<DOMAIN>') | |
if not zones or not zones.result: | |
raise Exception("zone not found") | |
zone_id = zones.result[0].id | |
dns_records = cf.dns.records.list(zone_id=zone_id) | |
for dns_record in dns_records.result: | |
dns_record_name = dns_record.name.replace(".<DOMAIN>", "") | |
if dns_record_name in ["<SUBDOMAIN>"]: | |
dns_record_id = dns_record.id | |
res = cf.dns.records.update( | |
dns_record_id=dns_record_id, | |
zone_id=zone_id, | |
name=dns_record.name, | |
type="A", | |
content=myip | |
) | |
if res.name: | |
print("Updated {} with IP {}.".format(res.name, myip)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment