Skip to content

Instantly share code, notes, and snippets.

@wannadrunk
Created December 13, 2024 16:36
Show Gist options
  • Save wannadrunk/0f321f9d4a75430bda90d256fc77ac88 to your computer and use it in GitHub Desktop.
Save wannadrunk/0f321f9d4a75430bda90d256fc77ac88 to your computer and use it in GitHub Desktop.
Python script to check the IP and update the DNS on Cloudflare.
#!/usr/bin/python3
import requests
from sendnotify import send_telegram_message
cfToken = 'API Token'
cfEmail = 'email@address'
cfZoneID = 'Zone ID'
cfRecordID = 'Record ID'
cfRecordName = 'domain name'
header = {"X-Auth-Email": cfEmail, "X-Auth-Key": cfToken, "Content-Type": "application/json"}
# check the IP address
response = requests.get("https://ipwho.is")
public_ip = response.json().get("ip")
# check the IP in Cloudflare
cfurl = f"https://api.cloudflare.com/client/v4/zones/{cfZoneID}/dns_records/{cfRecordID}"
response = requests.get(cfurl, headers=header)
reg_ip = response.json().get("result").get("content")
if public_ip != reg_ip:
payload = {"content": public_ip}
cfurl = f"https://api.cloudflare.com/client/v4/zones/{cfZoneID}/dns_records/{cfRecordID}"
response = requests.patch(cfurl, headers=header, json=payload)
send_telegram_message(f"The IP has been updated - {reg_ip} --> {public_ip}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment