修改解析记录
安装aliyun-python-sdk-alidns
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-alidns==2.6.32
代码如下
#!/usr/bin/env python
#coding=utf-8
from urllib import request as urlrequest
from json import loads
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkcore.auth.credentials import StsTokenCredential
from aliyunsdkalidns.request.v20150109.DescribeDomainRecordInfoRequest import DescribeDomainRecordInfoRequest
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
#Internet Ip
def myIp():
serverAddr = 'https://api.ipify.org/?format=json'
with urlrequest.urlopen(serverAddr) as f:
data = f.read().decode()
ip = loads(data)['ip']
return ip
#Record Ip
def originIp(response):
#response是str,字符串内容形如{'a':'b','c':'d'},截取原IP
str1 = response.decode('UTF-8')
str1 = str1.strip( '{}' )
str1 = str1.replace('"','')
print('str1: ')
print(type(str1))
print(str1)
print(str1.find("Value"))
str1List = str1.split(",")
str1Beg = str1.find("Value")
str1End = str1.find(",", str1Beg)
print('str1End: ')
print(str1End)
if str1Beg != -1 :
ip = str1[str1Beg+6:str1End]
else:
ip = '127.0.0.1'
print(ip)
return ip
#Internet Ipv6
def myIpv6():
serverAddr = 'https://api6.ipify.org/?format=json'
with urlrequest.urlopen(serverAddr) as f:
data = f.read().decode()
ip = loads(data)['ip']
#print(ip)
return ip
#Record Ipv6
def originIpv6(response):
str1 = response.decode('UTF-8')
print('ostr1: ')
str1 = str1.strip( '{}' )
str1 = str1.replace('"','')
print('str1Type: ')
print(type(str1))
print(str1)
print(str1.find("Value"))
str1List = str1.split(",")
str1Beg = str1.find("Value")
str1End = str1.find(",", str1Beg)
print('str1End: ')
print(str1End)
if str1Beg != -1 :
ip = str1[str1Beg+6:str1End]
else:
ip = '0.0.0.0'
print(ip)
return ip
def main():
DDRIrequest = DescribeDomainRecordInfoRequest()
DDRIrequest.set_accept_format('json')
DDRIrequest.set_RecordId("<RecordId>")
response = client.do_action_with_exception(DDRIrequest)
#pass response
#print(str(originIp(request), encoding=('utf-8')))
oIp = originIpv6(response)
mIp = myIpv6()
print('oIp:' + oIp)
print(type(oIp))
print('mIp:' + mIp)
print(type(mIp))
#判断原IP新IP是否相同
if mIp != oIp:
print('mIp not equal oIp')
changeIPv6(mIp)
else:
print('no need change IP')
def changeIP(myIp):
#change IP
UDRRequest = UpdateDomainRecordRequest()
UDRRequest.set_accept_format('json')
UDRRequest.set_RecordId("<RecordId>")
UDRRequest.set_RR("<RR>")
UDRRequest.set_Type("A")
UDRRequest.set_Value(myIp)
client.do_action_with_exception(UDRRequest)
response = client.do_action_with_exception(UDRRequest)
# python2: print(response)
print(str(response, encoding='utf-8'))
def changeIPv6(myIpv6):
#change IP
UDRRequest = UpdateDomainRecordRequest()
UDRRequest.set_accept_format('json')
UDRRequest.set_RecordId("20843827527950336")
UDRRequest.set_RR("laracart")
UDRRequest.set_Type("AAAA")
UDRRequest.set_Value(myIpv6)
client.do_action_with_exception(UDRRequest)
response = client.do_action_with_exception(UDRRequest)
# python2: print(response)
print(str(response, encoding='utf-8'))
credentials = AccessKeyCredential('<accessKeyId>', '<accessSecret>')
# use STS Token
# credentials = StsTokenCredential('<your-access-key-id>', '<your-access-key-secret>', '<your-sts-token>')
client = AcsClient(region_id='cn-hangzhou', credential=credentials)
main()
定时
cat jciot.timer
[Unit]
Description=Run jciot
[Timer]
OnBootSec=2m
OnUnitActiveSec=1m
[Install]
WantedBy=timers.target
cat jciot.service
[Unit]
Description=jciot service
[Service]
Type=simple
User=youlv
ExecStart=/usr/bin/python3 /opt/jciot.py
[Install]
WantedBy=multi-user.target