1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
| from aliyunsdkcore.client import AcsClient from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest import json import subprocess import re
def get_internet_ip(): child = subprocess.Popen("nslookup nideyuming", shell=True, stdout=subprocess.PIPE) out = child.communicate() ipv6_pattern = '(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})' m = re.findall(ipv6_pattern, str(out)) address = m[0][0] return address
def get_local_ip(): child = subprocess.Popen("ipconfig", shell=True, stdout=subprocess.PIPE) out = child.communicate() ipv6_pattern = '(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})' m = re.findall(ipv6_pattern, str(out)) address = m[1][0] return address
def get_recordid(client): request = DescribeDomainRecordsRequest() request.set_accept_format('json') request.set_DomainName("discover304.top") response = client.do_action_with_exception(request) response = str(response, encoding='utf-8') result = json.loads(response) recordid = result["DomainRecords"]["Record"][0]["RecordId"] return recordid
def update_record(client, priority, ttl, record_type, value, rr, record_id): request = UpdateDomainRecordRequest() request.set_accept_format('json') request.set_Priority(priority) request.set_TTL(ttl) request.set_Value(value) request.set_Type(record_type) request.set_RR(rr) request.set_RecordId(record_id) response = client.do_action_with_exception(request) response = str(response, encoding='utf-8') return response
def run_result(client, priority, ttl, record_type, value, rr, record_id): if get_local_ip() == get_internet_ip(): print("解析地址未发生变化,不修改!") pass else: result = update_record(client, priority, ttl, record_type, value, rr, record_id) result = json.loads(result) print("已更新解析地址,新的RecordId是:%s" % result["RecordId"])
if __name__ == '__main__': client = AcsClient('xxxx', 'xxxx', 'cn-beijing') record_id = get_recordid(client) ip = get_local_ip() run_result(client, "5", "600", "AAAA", ip, "@", record_id)
|