This repository has been archived on 2022-12-28. You can view files and clone it, but cannot push or open issues or pull requests.
GeDDNS/Code/ddns.py

181 lines
6.7 KiB
Python
Raw Normal View History

2020-01-26 07:00:02 +00:00
#!/usr/bin/env python
# #coding=utf-8
#---------------------------------------------------------------------------------
#Compliance with LGPLv3 protocol
#Help Document:https://www.alibabacloud.com/help
#Website:https://www.ghink.net
#Gitee:gitee.com/ghink
#Donate:afdian.net/@ghink
#---------------------------------------------------------------------------------
import os
import json
import requests
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import DescribeDomainRecordsRequest
2020-07-30 07:59:35 +00:00
from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import UpdateDomainRecordRequest
2020-01-26 07:00:02 +00:00
#---------------------------------------------------------------------------------
sets=open('set.json')#Open JSON File
setting = sets.read()#Read JSON File,Sent it in a Variable
sets.close()#Close JSON File
setload = json.loads(setting)#Load JSON
rr = setload['RR']#Save JSON in Variable
domain = setload['Domain']
accesskeyid = setload['accessKeyId']
accesssecret = setload['accessSecret']
ipv4 = setload['IPV4']
ipv6 = setload['IPV6']
#---------------------------------------------------------------------------------
if __name__ == '__main__':
2020-07-30 09:09:17 +00:00
v4api = 'http://ipv4.test-ipv6.com/ip/?callback='#Get IPV4 Address
v4jsonback = requests.get(url=v4api).text #IPV4 Address Variable"v4.text"
v4json = v4jsonback.replace("(","").replace(")","")
v4json = json.loads(v4json)
v4, sep, tail = v4json['ip'].partition(',')
2020-01-26 07:00:02 +00:00
print("Your IPV4 Address:")
2020-07-30 09:09:17 +00:00
print(v4)
v6api = 'http://ipv6.test-ipv6.com/ip/?callback='#Get IPV6 Address
v6jsonback = requests.get(url=v6api).text #IPV6 Address Variable"v6.text"
v6json = v6jsonback.replace("(","").replace(")","")
v6json = json.loads(v6json)
v6, sep, tail = v6json['ip'].partition(',')
2020-01-26 07:00:02 +00:00
print("Your IPV6 Address:")
2020-07-30 09:09:17 +00:00
print(v6)
2020-01-26 07:00:02 +00:00
#---------------------------------------------------------------------------------
client = AcsClient(accesskeyid, accesssecret, 'cn-hangzhou')#Aliyun AccessKey
#---------------------------------------------------------------------------------
if ipv4 == 'true':#Judging whether IPV4 DDNS is open
print('Your IPV4 DDNS is open.')
#Describe IPV4 Records
describev4 = DescribeDomainRecordsRequest()#Start Describe Domain Records
describev4.set_accept_format('json')
describev4.set_DomainName(domain)#Main Domain
describev4.set_RRKeyWord(rr)#Your RRKeyWord
describev4.set_TypeKeyWord("A")#Your Record Type
describebackv4 = client.do_action_with_exception(describev4)#Get Back JSON
# python2: print(response)
print(str(describebackv4, encoding='utf-8'))#Print JSON
#---------------------------------------------------------------------------------
#Read IPV4 JSON
recordlistv4=json.loads(describebackv4)#Read Back JSON
recordidv4=recordlistv4['DomainRecords']['Record'][0]['RecordId']#Get RecordId From Back JSON
print('Your IPV4 RecordId')
print(recordidv4)
valuev4=recordlistv4['DomainRecords']['Record'][0]['Value']#Get Value From Back JSON
print('Your IPV4 Value')
print(valuev4)
#RecordId For IPV4 Variable:"recoredidv4"
#Value For IPV4 Variable:"valuev4"
#---------------------------------------------------------------------------------
2020-07-30 09:09:17 +00:00
if valuev4 == v4:#Judging whether IPV4 address has changed
2020-01-26 07:00:02 +00:00
#No Change
print('Your IPV4 Record is Right,Noting to do.')
else:
#Changed Already
print('Your IPV4 Record has Changed,Update Now.')
#---------------------------------------------------------------------------------
#Update IPV4 Record
2020-07-30 07:55:34 +00:00
updv4 = UpdateDomainRecordRequest()#Start Update Domain Records
2020-01-26 07:00:02 +00:00
updv4.set_accept_format('json')
2020-07-30 07:59:35 +00:00
updv4.set_RecordId(recordidv4)
2020-07-30 09:09:17 +00:00
updv4.set_Value(v4)#Set Domain Record From Machine's Internet IPV4 Address
2020-01-26 07:00:02 +00:00
updv4.set_Type("A")#Set Domain Record Type
updv4.set_RR(rr)#Set Domain Record RR
updbackv4 = client.do_action_with_exception(updv4)#Get Back JSON
# python2: print(response)
print(str(updbackv4, encoding='utf-8'))#Print JSON
#---------------------------------------------------------------------------------
else:
print('Your have already closed IPV4 DDNS.')
#---------------------------------------------------------------------------------
if ipv6 == "true":#Judging whether IPV6 DDNS is open
#Describe IPV6 Records
describev6 = DescribeDomainRecordsRequest()#Start Describe Domain Records
describev6.set_accept_format('json')
describev6.set_DomainName(domain)#Main Domain
describev6.set_RRKeyWord(rr)#Your RRKeyWord
describev6.set_TypeKeyWord("AAAA")#Your Record Type
describebackv6 = client.do_action_with_exception(describev6)#Get Back JSON
# python2: print(response)
print(str(describebackv6, encoding='utf-8'))#Print JSON
#---------------------------------------------------------------------------------
#Read IPV6 JSON
recordlistv6=json.loads(describebackv6)#Read Back JSON
recordidv6=recordlistv6['DomainRecords']['Record'][0]['RecordId']#Get RecordId From Back JSON
print('Your IPV6 RecordId')
print(recordidv6)
valuev6=recordlistv6['DomainRecords']['Record'][0]['Value']#Get Value From Back JSON
print('Your IPV6 Value')
print(valuev6)
#RecordId For IPV6 Variable:"recoredidv6"
#Value For IPV6 Variable:"valuev6"
#---------------------------------------------------------------------------------
2020-07-30 09:09:17 +00:00
if valuev6 == v6:#Judging whether IPV6 address has changed
2020-01-26 07:00:02 +00:00
#No Change
print('Your IPV6 Record is Right,Noting to do.')
else:
#Changed Already
print('Your IPV6 Record has Changed,Update Now.')
#---------------------------------------------------------------------------------
#Update IPV6 Record
2020-07-30 07:55:34 +00:00
updv6 = UpdateDomainRecordRequest()#Start Update Domain Records
2020-01-26 07:00:02 +00:00
updv6.set_accept_format('json')
2020-07-30 07:59:35 +00:00
updv6.set_RecordId(recordidv6)
2020-07-30 09:09:17 +00:00
updv6.set_Value(v6)#Set Domain Record From Machine's Internet IPV6 Address
2020-01-26 07:00:02 +00:00
updv6.set_Type("AAAA")#Set Domain Record Type
updv6.set_RR(rr)#Set Domain Record RR
updbackv6 = client.do_action_with_exception(updv6)#Get Back JSON
# python2: print(response)
print(str(updbackv6, encoding='utf-8'))#Print JSON
#---------------------------------------------------------------------------------
else:
print('Your have already closed IPV6 DDNS.')
#---------------------------------------------------------------------------------