ddnsupdate.py
· 1.6 KiB · Python
Raw
import requests,time,socket,json,sys
def init_settings():
try:
with open("settings.json") as f:
settings = json.load(f)
return settings
except:
with open("settings.json", "w") as f:
settings = dict(domain="REPLACE_WITH_DOMAIN",
host="REPLACE_WITH_SUBDOMAIN",
password="REPLACE_WITH_DDNS_PASSWORD",
updateUrl="https://dynamicdns.park-your-domain.com/update")
f.write(json.dumps(settings))
return False
def do_update(settings):
with open("ddns.log", "a") as f:
settings["ip"] = requests.get("http://ipv4.icanhazip.com").text.strip()
oldip = socket.gethostbyname("{host}.{domain}".format(**settings))
if settings["ip"] != oldip:
d = requests.get(settings.pop("updateUrl"), params=settings).text
if "<Done>true</Done>" in d:
logline = "{}: Success ({})\r\n".format(time.ctime(), settings["ip"])
retval = 0
else:
logline = "{}: Error: {} ({})\r\n".format(time.ctime(), d, settings["ip"])
retval = 1
else:
logline = "{}: Skipping ({})\r\n".format(time.ctime(), settings["ip"])
retval = 5
f.write(logline)
print logline.strip()
return retval
def main():
settings = init_settings()
if settings:
return do_update(settings)
else:
print "settings.json updated with url parameters, please update to fit the url provided by your DDNS service"
return 50
if __name__ == "__main__":
sys.exit(main())
| 1 | import requests,time,socket,json,sys |
| 2 | |
| 3 | def 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 | |
| 17 | def 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 | |
| 36 | def 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 | |
| 44 | if __name__ == "__main__": |
| 45 | sys.exit(main()) |