dyndns-push 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/usr/bin/env python3
  2. myip_url = "https://YOUR_IP_FETCH_URL"
  3. dyndns_url = "https://YOUR_DYNDNS_UPDATE_URL"
  4. dyndns_domain = "EXAMPLE.COM"
  5. dyndns_user = "USER"
  6. dyndns_pw = "PASSWORD"
  7. v6netlen = 64
  8. import subprocess
  9. import ipaddress
  10. def wget(url, v4=None):
  11. args = ["wget", ]
  12. if v4 is not None:
  13. args.append("-4" if v4 else "-6")
  14. args.extend(["--quiet", "-O", "-", url])
  15. print(" ".join(args))
  16. p = subprocess.Popen(args, stdout=subprocess.PIPE)
  17. out, err = p.communicate()
  18. if p.returncode != 0 or not out:
  19. raise Exception("wget failed.")
  20. return out.decode("UTF-8")
  21. def getfield(raw, name):
  22. for line in raw.splitlines():
  23. if line.startswith(name + "="):
  24. return line[len(name) + 1 : ]
  25. return ""
  26. raw_v4 = wget(myip_url, v4=True)
  27. raw_v6 = wget(myip_url, v4=False)
  28. ipv4 = getfield(raw_v4, "REMOTE_ADDR")
  29. ipv6 = getfield(raw_v6, "REMOTE_ADDR")
  30. if not ipv4 and not ipv6:
  31. raise Exception("Got neither v4 nor v6 address.")
  32. dualstack = "1" if ipv4 and ipv6 else "0"
  33. if ipv6:
  34. adr = int(ipaddress.ip_address(ipv6))
  35. msk = ((1 << v6netlen) - 1) << (128 - v6netlen)
  36. ipv6pfx = f"{ipaddress.ip_address(adr & msk)}/{v6netlen}"
  37. else:
  38. ipv6pfx = ""
  39. r = wget(f"{dyndns_url}?"
  40. f"domain={dyndns_domain}&"
  41. f"user={dyndns_user}&"
  42. f"pw={dyndns_pw}&"
  43. f"ip4={ipv4}&"
  44. f"ip6={ipv6}&"
  45. f"ip6pfx={ipv6pfx}&"
  46. f"dualstack={dualstack}")
  47. print(f"result: {r.strip()}")