Created
June 18, 2019 12:33
-
-
Save sojournercntl/be6ecae96fcf46031af00f64777201d3 to your computer and use it in GitHub Desktop.
The following python script allows you to easily update the dynamic dns entry of a Namecheap domain using python and a cronjob.
This file contains 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
import urllib2 | |
import json | |
import xml.etree.ElementTree as Tree | |
# -------------------------------------------- | |
from time import strftime, gmtime | |
host = "https://myexternalip.com/json" | |
# -------------------------------------------- | |
dns_host = "" | |
dns_domain = "" | |
dns_key = "" | |
dns_request = "https://dynamicdns.park-your-domain.com/update?host="+dns_host+"&domain="+dns_domain+"&password="+dns_key+"&ip=" | |
def get_online_ip(): | |
ip = urllib2.urlopen(host).read() | |
data = json.loads(ip) | |
return data.get('ip') | |
def get_saved_ip(): | |
try: | |
with open("container-ip.tmp","r") as f: | |
return f.read() | |
except: | |
save_ip("0.0.0.0") | |
return "0.0.0.0" | |
def save_ip(ip): | |
with open("container-ip.tmp","w+") as f: | |
f.write(ip) | |
pass | |
def request_change(ip): | |
print("[X] - Challenge - New DNS Entry") | |
result = urllib2.urlopen(dns_request+ip).read() | |
tree = Tree.fromstring(result) | |
res_com = tree.find('Command').text | |
res_ip = tree.find('IP').text | |
res_errors = tree.find('ErrCount').text | |
res_done = tree.find('Done').text | |
if (res_com == 'SETDNSHOST') and (res_ip == ip) and (res_errors == str(0)) and (res_done == 'true'): | |
print("[X] - Challenge Successfull - DNS updated") | |
save_ip(ip) | |
else: | |
print("[ ] - Challenge Failed - DNS not updated") | |
print("[ --> "+result+" ]") | |
ip_online = "0.0.0.0" | |
save_ip(ip_online) | |
def timestamp_readable(): | |
return strftime("%Y-%m-%d %H:%M:%S", gmtime()) | |
ip_current = get_saved_ip() | |
ip_online = get_online_ip() | |
print "{"+timestamp_readable()+"} DNS UPDATE SERVICE - [CURRENT:"+ip_current+"] [ONLINE: "+ip_online+"]" | |
if ip_current != ip_online: | |
print "[X] - Requesting new IP: "+ip_online | |
request_change(ip_online) | |
else: | |
print "[ ] - Nothing changed" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment