阿里云DDns
1.安装ddns 所需环境
pip3 install aliyun-python-sdk-alidns
pip3 install aliyun-python-sdk-core
2.python程序
import json
import re
import socket
import subprocess
import time
import urllib.request
from threading import Thread
from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
from aliyunsdkcore.acs_exception.exceptions import (ClientException, ServerException)
from aliyunsdkcore.client import AcsClient
a = 'your AccessKey ID'
k = 'your Access Key Secret'
host_name = 'your host name'
def get_ipv6():
'''
获取本机的ipv6地址
'''
child = subprocess.Popen("ifconfig", shell=True, stdout=subprocess.PIPE)
out = child.communicate()
ipv6_patten = '(([a-f0-9]{1,4}:){7}[a-f0-9]{1,4})'
m = re.findall(ipv6_patten, str(out))
address = m[1][0]
return address
def update_ip(id, ipv6_addr,host_name, domain_type='AAAA', rr='*'):
'''
更新阿里云解析的ipv6地址
'''
client = AcsClient(a,k, 'cn-hangzhou')
request = UpdateDomainRecordRequest()
request.set_accept_format('json')
request.set_RecordId(id)
request.set_Value(ipv6_addr)
request.set_Type(domain_type)
request.set_RR(rr)
response = client.do_action_with_exception(request)
print(str(response, encoding='utf-8'))
def get_record_id(domain_name):
'''
获取domain_name的解析record_id,value
'''
client = AcsClient(a,k, 'cn-hangzhou')
request = DescribeDomainRecordsRequest()
request.set_accept_format('json')
request.set_DomainName(domain_name)
response = client.do_action_with_exception(request)
result = json.loads(str(response, encoding='utf-8'))
domian = result['DomainRecords']['Record'][0]
value = domian['Value']
RecordId = domian['RecordId']
return RecordId,value
def save_2_local(id,ipv6):
'''从服务器获取的ipv6地址存储到本地'''
f = open('ipv6.txt','w+')
f.write(str(id)+'#'+str(ipv6))
f.close()
def get_ipv6_for_local():
'''获取本地暂存的ipv6地址'''
f = open('ipv6.txt','r')
info = str(f.read())
f.close()
info = info.split("#")
# print(info)
fileipv6 = info[0]
id = info[1]
return id, fileipv6
def start_care():
while (True):
time.sleep(600)
if __name__ == "__main__":
RecordId,value = get_record_id(host_name)
fileipv6, id = get_ipv6_for_local()
nowipv6 = get_ipv6()
logger.info('当前ipv6~'+nowipv6+'/解析中~'+fileipv6)
if fileipv6 == nowipv6:
pass
else:
result = update_ip(id, nowipv6, host_name)
id = result['RecordId']
save_2_local(id,nowipv6)
linux 定时任务
执行 crontab -e
命令编辑定时任务
0/10 * * * * python3 /path/to/this.py