Skip to content

Instantly share code, notes, and snippets.

@epinna
Forked from j796160836/namecheap_ddns.py
Created November 4, 2018 10:54
Show Gist options
  • Save epinna/5406ab86833a34b79486899d99e707b9 to your computer and use it in GitHub Desktop.
Save epinna/5406ab86833a34b79486899d99e707b9 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!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment