Created
April 11, 2021 16:28
-
-
Save Nikro/d630920b7b036a4ce9199d6ac4c2b85e to your computer and use it in GitHub Desktop.
Dynamic DNS with Cloudflare
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/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