由 admin » 周五 6月 28, 2019 4:30 pm
今天又抽了点时间将IP更新程序优化了一下,调用方法:
python aliyundns.py www.atoo.top,代码如下:
# 阿里支DNS自动更新IP地址程序(Python3)
# 文件名 : aliyundns.py
# 调用方法: python3 aliyundns.py www.atoo.top
#!/usr/bin/env python3
#coding=utf-8
import sys
import json
import urllib.request
import datetime
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
from aliyunsdkalidns.request.v20150109.DescribeSubDomainRecordsRequest import DescribeSubDomainRecordsRequest
if len(sys.argv)<=1:
print( 'Usage: python3 aliyundna.py www.atoo.top')
sys.exit(0)
accessKeyId = 'LTAIjO23**PWBfI'
accessSecret = 'JlvkmaC***514aFM9Z14JGQbd8Zp'
DomainName = sys.argv[1]
# print sys.argv[0], sys.argv[1], sys.argv[2]
client = AcsClient(accessKeyId, accessSecret, 'cn-hangzhou')
request = DescribeSubDomainRecordsRequest()
request.set_accept_format('json')
request.set_SubDomain(DomainName)
response = client.do_action_with_exception(request)
# print response
response = json.loads(response.decode())
RecordId = response['DomainRecords']['Record'][0]['RecordId']
RR = response['DomainRecords']['Record'][0]['RR']
old_ip = response['DomainRecords']['Record'][0]['Value']
new_ip = urllib.request.urlopen("http://ip.42.pl/raw").read()
if old_ip != str(new_ip, 'utf-8'):
request = UpdateDomainRecordRequest()
request.set_accept_format('json')
request.set_Value(new_ip)
request.set_Type("A")
request.set_RR(RR)
request.set_RecordId(RecordId)
response = client.do_action_with_exception(request)
print( datetime.datetime.now(), ' ', DomainName,': ', old_ip, ' => ', new_ip )
# else :
# print datetime.datetime.now(), ' ', DomainName, ': IP has not changed.', new_ip
今天又抽了点时间将IP更新程序优化了一下,调用方法: [color=#FF0000]python aliyundns.py www.atoo.top[/color],代码如下:
[Codebox=python file=Untitled.py]
# 阿里支DNS自动更新IP地址程序(Python3)
# 文件名 : aliyundns.py
# 调用方法: python3 aliyundns.py www.atoo.top
#!/usr/bin/env python3
#coding=utf-8
import sys
import json
import urllib.request
import datetime
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
from aliyunsdkalidns.request.v20150109.DescribeSubDomainRecordsRequest import DescribeSubDomainRecordsRequest
if len(sys.argv)<=1:
print( 'Usage: python3 aliyundna.py www.atoo.top')
sys.exit(0)
accessKeyId = 'LTAIjO23**PWBfI'
accessSecret = 'JlvkmaC***514aFM9Z14JGQbd8Zp'
DomainName = sys.argv[1]
# print sys.argv[0], sys.argv[1], sys.argv[2]
client = AcsClient(accessKeyId, accessSecret, 'cn-hangzhou')
request = DescribeSubDomainRecordsRequest()
request.set_accept_format('json')
request.set_SubDomain(DomainName)
response = client.do_action_with_exception(request)
# print response
response = json.loads(response.decode())
RecordId = response['DomainRecords']['Record'][0]['RecordId']
RR = response['DomainRecords']['Record'][0]['RR']
old_ip = response['DomainRecords']['Record'][0]['Value']
new_ip = urllib.request.urlopen("http://ip.42.pl/raw").read()
if old_ip != str(new_ip, 'utf-8'):
request = UpdateDomainRecordRequest()
request.set_accept_format('json')
request.set_Value(new_ip)
request.set_Type("A")
request.set_RR(RR)
request.set_RecordId(RecordId)
response = client.do_action_with_exception(request)
print( datetime.datetime.now(), ' ', DomainName,': ', old_ip, ' => ', new_ip )
# else :
# print datetime.datetime.now(), ' ', DomainName, ': IP has not changed.', new_ip
[/Codebox]