Example #1
0
def domain_resolve_records_aliyun():
    '''
    返回三组数据:
    [[A记录],[解析IP],{"A记录": "解析IP"}]
    '''
    try:
        request = DescribeDomainRecordsRequest()
        request.set_accept_format('json')
        request.set_DomainName(DomainName)
        response = client.do_action_with_exception(request)
        # print(response)
        json_data = json.loads(str(response, encoding='utf-8'))
        # print(json_data)
        # return json_data
        total_data = []
        # 现有解析中的A记录
        domain_a_record_list_aliyun = []
        # 现有解析中的A记录IP
        ip_list_aliyun = []
        resolve_dict_aliyun = {}
        # 检查出有IP变化的域名,加入更新列表
        for i in json_data['DomainRecords']['Record']:
            ip_list_aliyun.append(i['Value'])
            domain_a_record_list_aliyun.append(i['RR'])
            resolve_dict_aliyun[i['RR']] = i['Value']
        total_data.append(domain_a_record_list_aliyun)
        total_data.append(ip_list_aliyun)
        total_data.append(resolve_dict_aliyun)

        return total_data

    except Exception as e:
        print(e)
Example #2
0
def _main(client, ip):
    from aliyunsdkalidns.request.v20150109.DescribeDomainRecordsRequest import *
    from aliyunsdkalidns.request.v20150109.UpdateDomainRecordRequest import *

    for rr, domain_name, type_ in config.RECORDS:
        req = DescribeDomainRecordsRequest()
        req.set_TypeKeyWord(type_)
        req.set_DomainName(domain_name)
        req.set_RRKeyWord(rr)
        records = json.loads(client.do_action_with_exception(req))['DomainRecords']['Record']

        if records:
            record = records[0]
            if record['Value'] == ip:
                logging.debug('%s.%s is already %s', rr, domain_name, ip)
            else:
                logging.info('Updating %s.%s from %s to %s',
                             rr, domain_name, record['Value'], ip)
                update = UpdateDomainRecordRequest()
                update.set_RecordId(record['RecordId'])
                update.set_RR(rr)
                update.set_Type(type_)
                update.set_Value(ip)
                client.do_action_with_exception(update)
        else:
            logging.warning('No such record %s.%s, skipping for now.',
                            rr, domain_name)
    records = docelem.getElementsByTagName("Record")
    for record in records:
        if record.getElementsByTagName("RR")[0].childNodes[0].data == RR:
            return {
                "RecordId":
                record.getElementsByTagName("RecordId")[0].childNodes[0].data,
                "Value":
                record.getElementsByTagName("Value")[0].childNodes[0].data
            }
    return None


rip = "\<code\>(?P<num>[\d\.]*)\</code>"
clt = client.AcsClient(keyId, keySecret, 'cn-hangzhou')
descDomainReq = DescribeDomainRecordsRequest()
descDomainReq.set_DomainName(domain)
descDomainRes = clt.do_action(descDomainReq)
recordInfo = getRecordInfo(descDomainRes, RR)
if recordInfo is None:
    raise Exception("can't find RR value " + RR)
print recordInfo
currentIP = recordInfo["Value"]
recordId = recordInfo["RecordId"]
while True:
    try:
        result = urllib2.urlopen('http://ip.cn/', timeout=3)
        text = result.read()
        ip = re.findall(rip, text)[0]
        if currentIP == ip:
            print time.strftime(ISOTIMEFORMAT, time.localtime(
            )) + " ip( %s ) is not change, sleep %ds" % (ip, updateInterval)
Example #4
0
#---------------------------------------------------------------------------------

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:')