Last active 1440821822

Script to update a DDNS provider (currently namecheap, but any provider that offers a simple http interface should work if the url is replaced) with your current IP

Revision 4d33b632eb4caa3d6d85570934a88fe3d96ff841

ddnsupdate.py Raw
1import requests,time,socket,json,sys
2
3def init_settings():
4 try:
5 with open("settings.json") as f:
6 settings = json.load(f)
7 return settings
8 except:
9 with open("settings.json", "w") as f:
10 settings = dict(domain="REPLACE_WITH_DOMAIN",
11 host="REPLACE_WITH_SUBDOMAIN",
12 password="REPLACE_WITH_DDNS_PASSWORD",
13 updateUrl="https://dynamicdns.park-your-domain.com/update")
14 f.write(json.dumps(settings))
15 return False
16
17def do_update(settings):
18 with open("ddns.log", "a") as f:
19 settings["ip"] = requests.get("http://ipv4.icanhazip.com").text.strip()
20 oldip = socket.gethostbyname("{host}.{domain}".format(**settings))
21 if settings["ip"] != oldip:
22 d = requests.get(settings.pop("updateUrl"), params=settings).text
23 if "<Done>true</Done>" in d:
24 logline = "{}: Success ({})\r\n".format(time.ctime(), settings["ip"])
25 retval = 0
26 else:
27 logline = "{}: Error: {} ({})\r\n".format(time.ctime(), d, settings["ip"])
28 retval = 1
29 else:
30 logline = "{}: Skipping ({})\r\n".format(time.ctime(), settings["ip"])
31 retval = 5
32 f.write(logline)
33 print logline.strip()
34 return retval
35
36def main():
37 settings = init_settings()
38 if settings:
39 return do_update(settings)
40 else:
41 print "settings.json updated with url parameters, please update to fit the url provided by your DDNS service"
42 return 50
43
44if __name__ == "__main__":
45 sys.exit(main())