Created
March 6, 2019 00:55
-
-
Save jangxx/52fdefc03370782bdc1270f1184dc0b0 to your computer and use it in GitHub Desktop.
Simple DynDNS update script
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 argparse, socket, requests, json, os | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--username', type=str, help="DynDns Username", required=True) | |
parser.add_argument('--password', type=str, help="DynDns Password", required=True) | |
parser.add_argument('--url', type=str, help="Update url with <ipaddr> and <ip6addr> parameters", required=True) | |
args = parser.parse_args() | |
# attempt to get the server's ipv4 and ipv6 addresses | |
addrs = socket.getaddrinfo('api.jangxx.com', 443) | |
ipv4_addrs = [addr[4][0] for addr in addrs if addr[0] == socket.AF_INET] | |
ipv6_addrs = [addr[4][0] for addr in addrs if addr[0] == socket.AF_INET6] | |
ipaddr = None | |
if len(ipv4_addrs) > 0: | |
ipv4_url = "http://" + str(ipv4_addrs[0]) + "/i/ip" | |
ipv4_req = requests.get(ipv4_url, headers={ "Host": "api.jangxx.com" }) | |
ipaddr = ipv4_req.text | |
ip6addr = None | |
if len(ipv6_addrs) > 0: | |
ipv6_url = "http://[" + str(ipv6_addrs[0]) + "]/i/ip" | |
ipv6_req = requests.get(ipv6_url, headers={ "Host": "api.jangxx.com" }) | |
ip6addr = ipv6_req.text | |
ipfile_exists = os.path.isfile("ips.json") | |
if ipfile_exists: | |
ipfile = open("ips.json", "r+") | |
try: | |
old_ips = json.load(ipfile) | |
except: | |
old_ips = { "ipaddr": None, "ip6addr": None } | |
ipfile.close() | |
ipfile = open("ips.json", "w+") | |
json.dump(old_ips, ipfile) | |
ipfile.close() | |
else: | |
ipfile = open("ips.json", "w+") | |
old_ips = { "ipaddr": None, "ip6addr": None } | |
json.dump(old_ips, ipfile) | |
ipfile.close() | |
# check if we need to update the ips | |
if (old_ips["ipaddr"] != ipaddr and ipaddr is not None) or (old_ips["ip6addr"] != ip6addr and ip6addr is not None): | |
print("Updating addresses on the server") | |
# assemble update url | |
update_url = args.url | |
if ipaddr: | |
update_url = update_url.replace("<ipaddr>", ipaddr) | |
if ip6addr: | |
update_url = update_url.replace("<ip6addr>", ip6addr) | |
resp = requests.get(update_url, auth=requests.auth.HTTPBasicAuth(args.username, args.password)) | |
print("Server reply:", resp.text) | |
# store updated addresses in the ips file | |
old_ips["ipaddr"] = ipaddr | |
old_ips["ip6addr"] = ip6addr | |
ipfile = open("ips.json", "w+") | |
json.dump(old_ips, ipfile) | |
ipfile.close() | |
else: | |
print("Nothing to update.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment