Last active
July 22, 2022 22:32
-
-
Save j796160836/316ef84a8c6b3a7c8234455d35b81f5c to your computer and use it in GitHub Desktop.
Python scripts for update DNS record for Namecheap (DDNS Services update script)
This file contains hidden or 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
#!/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
can you explain fully I have confused?