Skip to content

Instantly share code, notes, and snippets.

@Nikro
Created Apr 11, 2021
Embed
What would you like to do?
Dynamic DNS with Cloudflare
#!/usr/bin/python3
import requests
import ipaddress
import json
# Get these from cloudflare - check the blog article @ https://nikro.me/
token=""
zone=""
entry="cloud.nikro.me"
entry_id=""
cf_api_update = "https://api.cloudflare.com/client/v4/zones/{zone}/dns_records/{entry_id}"
cf_api_get = "https://api.cloudflare.com/client/v4/zones/{zone}/dns_records"
current_ip = requests.get('https://api.ipify.org').content.decode('utf-8')
if not entry_id:
print("Checking zone DNS details...")
res = requests.get(cf_api_update.format(zone=zone, entry_id=entry_id), headers={
'Authorization': 'Bearer %s' % (token),
'Content-Type': "application/json"
})
print("RESPONSE:")
print(json.dumps(res.json(), indent=4, sort_keys=True))
elif ipaddress.ip_address(current_ip):
print("IP identified: %s" % (current_ip))
print("Updating... %s with %s" % (entry, current_ip))
res = requests.put(cf_api_update.format(zone=zone, entry_id=entry_id), headers={
'Authorization': 'Bearer %s' % (token),
'Content-Type': "application/json"
}, json={
"type": "A",
"name": entry,
"content": current_ip,
"ttl": "180",
"proxied": False
})
print("RESPONSE:")
print(json.dumps(res.json(), indent=4, sort_keys=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment