Skip to content

Instantly share code, notes, and snippets.

@theskanthunt42
Last active March 26, 2022 17:19
Show Gist options
  • Save theskanthunt42/c38320e74cc3ae917fb8b362e8089cc5 to your computer and use it in GitHub Desktop.
Save theskanthunt42/c38320e74cc3ae917fb8b362e8089cc5 to your computer and use it in GitHub Desktop.
A automatic DDNS script using cloudflare api and etc, Please review the script yourself before you use(If there is)
#Script for building up DDNS service using Cloudflare and https://ip.sb or http://icanhazip.com/
import requests
import time
import json
update_interval = 21600 #Update interval, default to 6hr, aka 21600s
api_token = "" #your cf global api key
email_address = "" #your email address
target_domain = "" #e.g gist.github.com
domain = target_domain.split(".")[1] + "." + target_domain.split(".")[2]
identifiyer = ""
record_type = "A"
ttl = "1"
#ip_type = "ipv4"
ip_provider = "http://ipv4.icanhazip.com/"
#Auto: http://icanhazptr.com/ and https://api.ip.sb/ip (Not recommend since if u got a ipv6 this will only return the ipv6 one)
#IPv4 only: http://ipv4.icanhazip.com/ and https://api-ipv4.ip.sb/ip
#IPv6 only: http://ipv6.icanhazip.com/ and https://api-ipv6.ip.sb/ip
#try:
# get_ip = requests.get(f"https://api-{ip_type}.ip.sb/ip")
#except get_ip.status_code != 200:
# print("Cannot retrive your ip address from ip.sb.")
def updateIP():
print("\nGetting IP from provider...")
get_ip = requests.get(ip_provider)
ip = get_ip.text.strip("\n")
print(f"IP address: {ip}")
print("Getting Zone ID from Cloudflare...")
get_zoneid = requests.get(f"https://api.cloudflare.com/client/v4/zones?name={domain}", headers={"X-Auth-Email":email_address, "X-Auth-Key":api_token,"Content-Type":"application/json"})
zone_id = get_zoneid.json()['result'][0]['id'] #I'm lazy on this, Change if you got more than one domain in your cf
print(f"Zone ID: {zone_id}")
print("Getting DNS Record ID...")
get_record_id = requests.get(f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records?name={target_domain}", headers={"X-Auth-Email":email_address, "X-Auth-Key":api_token,"Content-Type":"application/json"})
#print(get_record_id.text)
record_id = get_record_id.json()['result'][0]['id']
#print(record_id.text)
print(f"DNS Record ID: {record_id}")
json_payload = {
'id':zone_id,
'type':record_type,
'name':target_domain,
'content':ip,
'ttl':ttl,
'proxied':False
}
payload = json.dumps(json_payload)
cf_headers = {
'X-Auth-Email':email_address,
'X-Auth-Key':api_token,
'Content-Type':'application/json'
}
cf_put = requests.put(f"https://api.cloudflare.com/client/v4/zones/{zone_id}/dns_records/{record_id}", headers=cf_headers, data=payload)
if cf_put.status_code == 200:
print("Success!")
else:
print(f"There is sth wrong...\nError code: {cf_put.status_code}\nResponse: {cf_put.text}\nRequest header: {cf_headers}")
def main():
while True:
updateIP()
print(f"Gonna sleep for the next {update_interval} Seconds")
time.sleep(update_interval)
if __name__ == "__main__":
main()
@theskanthunt42
Copy link
Author

Typical $h!t code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment