User Tools

Site Tools


library:linux:aliyun_ddns

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
library:linux:aliyun_ddns [2022/08/11 10:31]
lhaosen [2. With Configuration file]
library:linux:aliyun_ddns [2022/08/11 11:06] (current)
lhaosen [2. With Configuration file]
Line 118: Line 118:
 ===== - With Configuration file ===== ===== - With Configuration file =====
 <code python ddns.py> <code python ddns.py>
 +#!/usr/bin/python3
 +#coding=utf-8
  
 +#pip3 install aliyun-python-sdk-core-v3
 +#pip3 install aliyun-python-sdk-alidns==2.0.6
 +
 +import sys
 +import json
 +
 +from aliyunsdkcore.client import AcsClient
 +from aliyunsdkalidns.request.v20150109 import \
 +    DescribeSubDomainRecordsRequest, \
 +    AddDomainRecordRequest, \
 +    UpdateDomainRecordRequest, \
 +    DeleteDomainRecordRequest
 +
 +with open("ddns.json","r") as json_file:
 +    json_dict = json.load(json_file)
 +    #print(json_dict)
 +    #print("type(json_dict) = >", type(json_dict))
 +    #print(json.dumps(json_dict, indent=4))
 +
 +    ID = json_dict["ID"
 +    SECRET = json_dict["SECRET"]
 +    regionId = json_dict["regionId"]
 +    domainName = json_dict["domainName"]
 +    subDomain = json_dict["subDomain"]
 +
 +
 +#print('Register Domain: %s.%s'%(subDomain,domainName) )
 +#print('ID:', ID)
 +#print('SECRET:', SECRET)
 +#print('regionId:', regionId)
 +
 +
 +def getTemporaryIPv6Address():
 +    import requests
 +    import time
 +    
 +    while True:
 +        try:
 +            r = requests.get('https://v6.ident.me', timeout = 20)
 +            
 +            if r.status_code == 200 :
 +                ipv6 = r.text
 +            else:
 +                ipv6 = ''
 +
 +            break
 +        except:
 +            print('Request exception.')
 +            pass
 +    
 +        time.sleep(5)
 +    
 +    return ipv6
 +
 +
 +def getDomainInfo(domain,Type):
 +  request = DescribeSubDomainRecordsRequest.DescribeSubDomainRecordsRequest()
 +  request.set_accept_format('json')
 +
 +  request.set_Type(Type)
 +
 +  request.set_SubDomain(domain)
 +
 +  response = client.do_action_with_exception(request)
 +  response = str(response, encoding='utf-8')
 +
 +  return json.loads(response)
 +
 +def updateDomainRecordEx(client,ip,subdomain,record_id,recordtype='A'):
 +  request = UpdateDomainRecordRequest.UpdateDomainRecordRequest()
 +  request.set_accept_format('json')
 +
 +  # request.set_Priority('1')
 +  request.set_TTL('600')
 +  request.set_Value(ip) # 新的ip地址
 +  request.set_Type(recordtype)
 +  request.set_RR(subdomain)
 +  request.set_RecordId(record_id) # 更新记录需要指定 record_id ,该字段为记录的唯一标识,可以在获取方法的返回信息中得到该字段的值
 +
 +  response = client.do_action_with_exception(request)
 +  response = str(response, encoding='utf-8')
 +  return response
 +
 +
 +if __name__ == "__main__":
 +    hostIPv6 = getTemporaryIPv6Address()
 +    typeIPv6 = 'AAAA'
 +
 +
 +    if hostIPv6 != '':
 +        print('IPv6 Address: %s'%(hostIPv6))
 +    else:
 +        print('Fail to get host IPv6 address.')
 +        sys.exit(1)
 +
 +    client = AcsClient(ID, SECRET, regionId)
 +
 +    domain = subDomain + '.' + domainName;
 +    info = getDomainInfo(domain,typeIPv6)
 +    
 +    if info["TotalCount"] == 1: 
 +        originalIPv6 = info["DomainRecords"]["Record"][0]["Value"]
 +       
 +        if hostIPv6 == originalIPv6:
 +            print( "%s  %s   (no change)"%(domain.ljust(24,' '),originalIPv6.ljust(40,' ')))
 +        else:
 +            record_id = info["DomainRecords"]["Record"][0]["RecordId"]
 +            update_result = updateDomainRecordEx(client,hostIPv6,subDomain,record_id,typeIPv6)
 +            print( "%s  %s-> %s"%(domain.ljust(24,' '),originalIPv6.ljust(40,' '),hostIPv6))
 +    else:
 +        print('Fail to get information of %s'%(domain,))
 +        sys.exit(2)
 +        
 +    sys.exit(0)
 +    
 </code> </code>
  
 <code json ddns.json> <code json ddns.json>
 +
 { {
   "domainName" : "synce.cn",   "domainName" : "synce.cn",
   "subDomain" : "z800",   "subDomain" : "z800",
-  "ID": "xxxxxxxx", +  "regionId" : "cn-hangzhou", 
-  "SECRET": "xxxxxxxx"+  "ID": "xxxxxxxxxx", 
 +  "SECRET": "xxxxxxxxxx"
 } }
  
 </code> </code>
library/linux/aliyun_ddns.1660185069.txt.gz · Last modified: 2022/08/11 10:31 by lhaosen