阿里云修改解析记录

本文详细介绍了如何使用aliyun-python-sdk-alidns进行DNS记录的获取、修改和IPv6切换,包括设置AccessKey或STS Token,调用DescribeDomainRecordInfo和UpdateDomainRecord API。
摘要由CSDN通过智能技术生成

修改解析记录

安装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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值