Created
April 19, 2022 12:15
-
-
Save Earlopain/a3d04f04cd0606e3d7c3d71ea308b1cc to your computer and use it in GitHub Desktop.
Script to update cloudflare dns entries
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 requests | |
zone_id = "" | |
api_key= "" | |
headers = { "Content-Type": "application/json", "Authorization": f'Bearer {api_key}'} | |
current_dns = requests.get(f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records?type=A', headers=headers).json() | |
if not current_dns["success"]: | |
print("Failed to get current ip") | |
exit() | |
current_cloudflare_ip = current_dns["result"][0]["content"] | |
current_ip = requests.get("https://checkip.amazonaws.com").text.strip() | |
if current_cloudflare_ip == current_ip: | |
print(f'Ip is still {current_ip}, quiting') | |
exit() | |
print(f'cloudflare: {current_cloudflare_ip}, actual: {current_ip}') | |
for entry in current_dns["result"]: | |
response = requests.patch(f'https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{entry["id"]}', headers=headers, json={"content": current_ip}).jso> if not response["success"]: | |
print(f'Failed to update {entry["name"]}') | |
print(response) | |
else: | |
print(f'Updated {entry["name"]}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment