Created
June 10, 2019 02:04
-
-
Save benzBrake/08c4096cecf26fcbf45f90d67b3109c1 to your computer and use it in GitHub Desktop.
DNSPod DDNS Script Python Version
This file contains hidden or 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
| #!/usr/bin/env python2 | |
| # -*- coding:utf-8 -*- | |
| import httplib, urllib | |
| import os, time, json | |
| domain_ids = dict() | |
| ddns_domain = "" | |
| ddns_subdomain = "" | |
| ddns_type="AAAA" # AAAA or A | |
| base_params = dict( | |
| login_token="id,token", # replace with your api id & token | |
| format="json" | |
| ) | |
| def get_post_response(uri, params, headers={"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}): | |
| conn = httplib.HTTPSConnection("dnsapi.cn") | |
| conn.request("POST", uri, urllib.urlencode(params), headers) | |
| response = conn.getresponse() | |
| data = response.read() | |
| conn.close() | |
| return data | |
| def get_domain_list(): | |
| return json.loads(get_post_response("/Domain.List", base_params))['domains'] | |
| def get_domain_id(str): | |
| if str == None: | |
| print ("[ERROR] get_domain_id: parameter error!") | |
| return None | |
| if domain_ids != None and domain_ids.has_key(str): | |
| return domain_ids[str] | |
| domain_list = get_domain_list() | |
| for obj in domain_list: | |
| if obj['punycode'] == str: | |
| domain_ids[str] = obj['id'] | |
| return obj['id'] | |
| return None | |
| def get_subdomain_records(domain, sub_domain, type): | |
| if domain == None or sub_domain == None or type == None: | |
| print ("[ERROR] get_subdomain_records: parameter error!") | |
| return None | |
| domain_id = get_domain_id(domain) | |
| params = base_params | |
| params['domain_id'] = domain_id | |
| params['sub_domain'] = sub_domain | |
| params['record_type'] = type | |
| data = json.loads(get_post_response("/Record.List", params)) | |
| return data | |
| def get_old_record(domain, sub_domain, type): | |
| if domain == None or sub_domain == None or type == None: | |
| print ("[ERROR] get_old_records: parameter error!") | |
| return None | |
| records = get_subdomain_records(domain, sub_domain, type) | |
| if not records.has_key("records"): | |
| return None | |
| if len(records) > 0: | |
| return records['records'][0] | |
| else: | |
| print ("[INFO] get_old_records: No records!") | |
| return None | |
| def add_record(domain, sub_domain, type, record): | |
| if domain == None or sub_domain == None or type == None or record == None: | |
| print ("[ERROR] add_record: parameter error!") | |
| params = base_params | |
| params['domain'] = domain | |
| params['sub_domain'] = sub_domain | |
| params['record_line_id'] = 0 | |
| params['value'] = record | |
| params['record_type'] = type | |
| return get_post_response("/Record.Create", params) | |
| def edit_record(domain, sub_domain, record_id, record_type, record): | |
| if domain == None or sub_domain == None or record_id == None or record_type == None or record == None: | |
| print ("[ERROR] add_record: parameter error!") | |
| params = base_params | |
| params['domain'] = domain | |
| params['record_id'] = record_id | |
| params['sub_domain'] = sub_domain | |
| params['record_type'] = record_type | |
| params['record_line_id'] = 0 | |
| params['value'] = record | |
| return get_post_response("/Record.Modify", params) | |
| def getip_from_ipsb(type): | |
| d = dict(A="4", AAAA="6") | |
| if type == "A" or type == "AAAA": | |
| process = os.popen('curl -sSL -' + d[type] + ' ip.sb') | |
| output = process.read() | |
| process.close() | |
| return output | |
| else: | |
| print ("[ERROR] getip_from_ipsb: parameter error!") | |
| return None | |
| def ddns(domain, sub_domain, record_type): | |
| old_record = get_old_record(domain, sub_domain, record_type) | |
| now_ip = getip_from_ipsb(record_type) | |
| if old_record != None: | |
| record_id = old_record['id'] | |
| return edit_record(domain, sub_domain, record_id, record_type, now_ip) | |
| else: | |
| return add_record(domain, sub_domain, record_type, now_ip) | |
| if __name__ == '__main__': | |
| print ddns(ddns_domain, ddns_subdomain, ddns_type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment