Skip to content

Instantly share code, notes, and snippets.

@j796160836
Last active July 22, 2022 22:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save j796160836/316ef84a8c6b3a7c8234455d35b81f5c to your computer and use it in GitHub Desktop.
Save j796160836/316ef84a8c6b3a7c8234455d35b81f5c to your computer and use it in GitHub Desktop.
Python scripts for update DNS record for Namecheap (DDNS Services update script)
#!/usr/bin/env python
# encoding: utf-8
# Setup introductions:
# Open Namecheap website, select a domain (e.g. abc.com) then go to Advanced DNS
# (Accounts > Domain List > Advanced DNS)
# Insert an "A + Dynamic DNS Record", with hostname (e.g. my) and type whatnever IP address.
# Edit scripts for proper HOSTNAME (e.g. my.abc.com) and APIKEY (Dynamic DNS Password).
# Run and have fun!
import requests
import io, json
import certifi
from xml.etree import ElementTree
HOSTNAME="YOUR_HOSTNAME" # Namecheap hostname (including subdomain)
APIKEY="YOUR_DDNS_APIKEY" # Namecheap DDNS Token (Accounts > Domain List > Advanced DNS)
def getIP():
r = requests.get("https://ifconfig.co/json", verify=certifi.where()).json()
return r['ip']
def updateRecord(ip):
global HOSTNAME
global APIKEY
d = HOSTNAME.find('.')
host = HOSTNAME[:d]
domain = HOSTNAME[(d+1):]
# DO NOT change the url "dynamicdns.park-your-domain.com". It's vaild domain provide by namecheap.
return requests.get("https://dynamicdns.park-your-domain.com/update?host=" + host + "&domain=" + domain + "&password=" + APIKEY + "&ip=" + ip, verify=certifi.where())
ip = getIP()
print("External IP: " + ip)
r = updateRecord(ip)
errCount = ElementTree.fromstring(r.content).find("ErrCount").text
if int(errCount) > 0:
print("API error\n" + r.content)
else:
print("Updete IP success!")
@jayeshrd
Copy link

can you explain fully I have confused?

@rasmuslinden
Copy link

Thank you! works great!

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