首页 » 互联网资讯 » 正文

Godaddy DDNS Python开源脚本

脚本,Python3…

  1. import json
  2. import re
  3. import os
  4. from sys import exit
  5. from urllib import request
  6. from urllib.error import HTTPError
  7. # Begin settings
  8. # Get the Production API key/secret from https://developer.godaddy.com/keys/.
  9. # Ensure it’s for “Production” as first time it’s created for “Test”.
  10. KEY = None  #
  11. SECRET = None  #
  12. # set call API header
  13. HEADERS = {
  14.     “Accept”: “application/json”,
  15.     ‘Content-type’: ‘application/json’,
  16.     ‘Authorization’: ‘sso-key {}:{}’.format(KEY, SECRET)
  17. }
  18. # Domain to update.
  19. DOMAIN = None  #
  20. # Advanced settings – change only if you know what you’re doing :-)
  21. # Record type, as seen in the DNS setup page, default A.
  22. TYPE = ‘A’
  23. # Record name, as seen in the DNS setup page, default @.
  24. NAME = ‘@’
  25. # Time To Live in seconds, minimum default 600 (10mins).
  26. # If your public IP seldom changes, set it to 3600 (1hr) or more for DNS servers cache performance.
  27. TTL = 600
  28. # godaddy API URL
  29. GOD_ADDY_API_URL = “https://api.godaddy.com/v1/domains/{}/records/{}/{}”.format(DOMAIN, TYPE, NAME)
  30. # Writable path to last known Public IP record cached. Best to place in tmpfs.
  31. CACHED_IP_FILE = ‘/tmp/current_ip’
  32. CACHED_IP = None
  33. if os.path.isfile(CACHED_IP_FILE):
  34.     CACHED_IP = open(CACHED_IP_FILE, mode=”r”, encoding=”utf-8″).read()
  35. # External URL to check for current Public IP, must contain only a single plain text IP.
  36. # Default http://api.ipify.org.
  37. CHECK_URL = ‘http://api.ipify.org’
  38. if not KEY or not SECRET:
  39.     print(“Error: Requires API ‘Key/Secret’ value.”)
  40.     exit(1)
  41. if not DOMAIN:
  42.     print(“Error: Requires ‘Domain’ value.”)
  43.     exit(1)
  44. # Get Host Public IP
  45. PUBLIC_IP = request.urlopen(CHECK_URL).read().decode(‘utf-8’)
  46. regex = r”^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$”
  47. if re.search(regex, PUBLIC_IP):
  48.     print(“Get Public IP: {}”.format(PUBLIC_IP))
  49. else:
  50.     print(“Fail! Public IP: {}”.format(PUBLIC_IP))
  51.     exit(1)
  52. # Check if the IP needs to be updated
  53. if CACHED_IP != PUBLIC_IP:
  54.     req = request.Request(url=GOD_ADDY_API_URL, headers=HEADERS)
  55.     try:
  56.         with request.urlopen(req) as response:
  57.             data = json.loads(response.read().decode(‘utf-8’))
  58.             NAME_BIND_IP = data[0][“data”] if data else None
  59.     except HTTPError as e:
  60.         if e.code in (422, 404):
  61.             NAME_BIND_IP = None
  62.     if NAME_BIND_IP == PUBLIC_IP:
  63.         print(“unchanged! Current ‘Public IP’ matches ‘GoDaddy’ records. No update required!”)
  64.     else:
  65.         print(“changed! Updating ‘{}.{}’, {} to {}”.format(NAME, DOMAIN, NAME_BIND_IP, PUBLIC_IP))
  66.         data = json.dumps([{“data”: PUBLIC_IP, “name”: NAME, “ttl”: TTL, “type”: TYPE}]).encode(‘utf-8’)
  67.         req = request.Request(url=GOD_ADDY_API_URL, data=data, headers=HEADERS, method=’PUT’)
  68.         with request.urlopen(req) as response:
  69.             print(“Success!” if not response.read().decode(‘utf-8’) else “Success!”)
  70.             open(‘/tmp/current_ip’, mode=”w”, encoding=”utf-8″).write(PUBLIC_IP)
  71. else:
  72.     print(“Current ‘Public IP’ matches ‘Cached IP’ recorded. No update required!”)

README

  1. mkdir script
  2. cd script/
  3. wget https://raw.githubusercontent.com/anshengme/ddns/master/godaddy_for_python.py
  4. crontab -e
  5. */5 * * * * /usr/bin/python3 /root/script/godaddy_for_python.py >> /var/log/godaddy_for_python.py.log

贴上GitHub: https://github.com/anshengme/ddns



四川网友 流星角落 评论:
好东西,收藏了


贵州网友 多身五样传 评论:
一般不是到手cf么 、
不过也谢谢大佬


湖南网友 一滴泪的邂逅1 评论:

一般不是到手cf么 、
不过也谢谢大佬

我的域名现在放在狗爹,所以就弄狗爹的了,如果是CF,到时候在撸个CF就好了

发表评论