Ejemplo n.º 1
0
def describe_records():
    try:
        logging.info('DescribeSubDomainRecordsRequest %s' % SUBDOMAIN)
        request = DescribeSubDomainRecordsRequest()
        request.set_DomainName(DOMAIN)
        request.set_SubDomain(SUBDOMAIN)
        request.set_Type('AAAA')
        request.set_accept_format('json')
        response = acsClient.do_action_with_exception(request)
        return json.loads(response)['DomainRecords']['Record'][0]['RecordId']
    except Exception as e:
        logging.error(e)
Ejemplo n.º 2
0
def get_record():
    get_record_id = None
    try:
        request = DescribeSubDomainRecordsRequest()
        request.set_SubDomain('%s.%s' % (SUBDOMAIN, DOMAIN))
        request.set_Type('AAAA')
        request.set_DomainName(DOMAIN)
        request.set_accept_format('json')
        response = acsClient.do_action_with_exception(request)
        get_record_id = json.loads(
            response)['DomainRecords']['Record'][0]['RecordId']
    except Exception as e:
        print('get_record : %s' % e)
    return get_record_id
Ejemplo n.º 3
0
Archivo: ddns.py Proyecto: oratun/ddns
def update_domain(domain, current_ip):
    request = DescribeSubDomainRecordsRequest()
    sub_domain = domain['RR'] + '.' + domain['DomainName']
    print(sub_domain)
    request.set_SubDomain(sub_domain)
    response = client.do_action_with_exception(request)
    r = json.loads(response)
    print(datetime.datetime.now(), r)

    records = r['DomainRecords']['Record']
    if records:
        # 存在记录
        record = records[0]
        if record['Value'] != current_ip:
            update_request = UpdateDomainRecordRequest()
            # 主机记录
            update_request.set_RR(domain['RR'])
            # 记录ID
            update_request.set_RecordId(record['RecordId'])
            # 主机记录值设为当前主机IP
            update_request.set_Value(current_ip)
            # 解析记录类型
            update_request.set_Type(domain['Type'])
            update_response = client.do_action_with_exception(update_request)
            print(json.loads(update_response))
Ejemplo n.º 4
0
 def getinfo(Domain, Name):
     from aliyunsdkalidns.request.v20150109.DescribeSubDomainRecordsRequest import DescribeSubDomainRecordsRequest
     request = DescribeSubDomainRecordsRequest()
     request.set_accept_format("json")
     request.set_DomainName(Domain)
     request.set_SubDomain(Name + "." + Domain)
     response = DDNSUnit.response(request)
     info = json.loads(response)
     return info
Ejemplo n.º 5
0
def aliyun_get_subdomain(client, subdomain):
    request = None
    response = None
    request = DescribeSubDomainRecordsRequest()
    request.set_SubDomain(subdomain)
    try:
        response = client.do_action_with_exception(request)
    except ServerException as e:
        print('Server Error! Domain: {}, Code: {}.'.format(
            subdomain, e.get_error_code()))
        print(e.get_error_msg())
        return None
    except ClientException as e:
        print('Client Error! Domain: {}, Code: {}.'.format(
            subdomain, e.get_error_code()))
        print(e.get_error_msg())
        return None
    return json.loads(str(response, encoding='utf-8'))
Ejemplo n.º 6
0
def Describe_SubDomain_Records(client, record_type, subdomain):
    request = DescribeSubDomainRecordsRequest()
    request.set_accept_format('json')

    request.set_Type(record_type)
    request.set_SubDomain(subdomain)

    response = client.do_action_with_exception(request)
    response = str(response, encoding='utf-8')
    relsult = json.loads(response)
    return relsult
Ejemplo n.º 7
0
    def _find_txt_record_id(self, domain, record_name, record_content):
        """
        Find the record_id for a TXT record with the given name and content.

        :param str domain: The domain to use to look up the Aliyun zone.
        :param str record_name: The record name (typically beginning with '_acme-challenge.').
        :param str record_content: The record content (typically the challenge validation).
        :returns: The record_id, if found.
        :rtype: str
        """

        request = DescribeSubDomainRecordsRequest()
        request.set_accept_format('json')

        request.set_SubDomain(record_name)

        try:
            response = self.ac.do_action_with_exception(request)
        except ClientException as e:
            logger.warning(
                'Encountered Aliyun ClientException described domain records: %s',
                e)
            return None

        for record in json.loads(response)['DomainRecords']['Record']:
            if record["RR"] + '.' + record["DomainName"] == record_name and record["Type"] == "TXT" \
               and record["Value"] == record_content:
                return record["RecordId"]

        return None
Ejemplo n.º 8
0
    def describe_subdomain_records(self, record_type, subdomain):
        logging.info("子域名解析记录查询")
        from aliyunsdkalidns.request.v20150109.DescribeSubDomainRecordsRequest import DescribeSubDomainRecordsRequest
        request = DescribeSubDomainRecordsRequest()

        request.set_accept_format('json')
        request.set_Type(record_type)
        request.set_SubDomain(subdomain)

        response = self.client.do_action_with_exception(request)
        response = str(response, encoding='utf-8')
        result = json.loads(response)
        logging.debug(result)
        return result
Ejemplo n.º 9
0
def DescribeSDR(X):
    request = DescribeSubDomainRecordsRequest()
    request.set_accept_format('json')
    request.set_SubDomain(X)
    response = client.do_action_with_exception(request)
    return (json.loads(str(
        response, encoding='utf-8')).get('DomainRecords').get('Record'))[0]
Ejemplo n.º 10
0
def list_domain_records(client, domain_name):
    request = DescribeSubDomainRecordsRequest()
    request.set_accept_format('json')
    request.set_SubDomain(domain_name)
    response = client.do_action_with_exception(request)
    response = response.decode()
    return json.loads(response)['DomainRecords']['Record']
Ejemplo n.º 11
0
 def record_id(self, sub_domain):
     """
     :param sub_domain:
     :return:
     """
     request = DescribeSubDomainRecordsRequest()
     request.set_accept_format('json')
     request.set_SubDomain(sub_domain)
     try:
         logging.info("Start to find domain record id for sub domain %s." %
                      sub_domain)
         response = self.client.do_action_with_exception(request)
         if ResponseUtil.success(response):
             record = get_domain_record_id(str(response, encoding='utf-8'))
             if not record:
                 raise ValueError("Record ID NullPointException")
             logging.info("Found domain record id %s for sub_domain : %s." %
                          (record, sub_domain))
             return True, record
         else:
             return False, str(response, encoding='utf-8')
     except Exception as e:
         logging.error(
             "Failed to get description for domain %s" % sub_domain, e)
         return False, e
Ejemplo n.º 12
0
def get_sub_domain_records(client, subdomain_name):
    """subdomain_name: eg. `www.cheng10.cc`, `home.cheng10.cc`
    """
    request = DescribeSubDomainRecordsRequest()
    request.set_accept_format('json')

    request.set_SubDomain(subdomain_name)

    response = client.do_action_with_exception(request)
    res = json.loads(str(response, encoding='utf-8'))
    record_info = res['DomainRecords']['Record'][0]
    logger.debug(f'record found: {record_info}')
    return record_info
    # sample response data
    """
Ejemplo n.º 13
0
    response = client.do_action_with_exception(request)


def add(DomainName, RR, Type, Value):  # 添加新的域名解析记录
    from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest
    request = AddDomainRecordRequest()
    request.set_accept_format('json')
    request.set_DomainName(DomainName)
    request.set_RR(RR)
    request.set_Type(Type)
    request.set_Value(Value)
    response = client.do_action_with_exception(request)


if ipv4_flag == 1:
    request = DescribeSubDomainRecordsRequest()
    request.set_accept_format('json')
    request.set_DomainName(domain)
    request.set_SubDomain(name_ipv4 + '.' + domain)
    response = client.do_action_with_exception(request)  # 获取域名解析记录列表
    domain_list = json.loads(response)  # 将返回的JSON数据转化为Python能识别的

    ip = urlopen('https://api-ipv4.ip.sb/ip').read()  # 使用IP.SB的接口获取ipv4地址
    ipv4 = str(ip, encoding='utf-8')
    print("获取到IPv4地址:%s" % ipv4)

    if domain_list['TotalCount'] == 0:
        add(domain, name_ipv4, "A", ipv4)
        print("新建域名解析成功")
    elif domain_list['TotalCount'] == 1:
        if domain_list['DomainRecords']['Record'][0]['Value'].strip(
Ejemplo n.º 14
0
    def do(self):
        domain = self._domain
        sub_domain = self._sub_domain
        ipv4_out = ""
        result = False
        if ipv4_flag == 1:
            request = DescribeSubDomainRecordsRequest()
            request.set_accept_format('json')
            request.set_DomainName(domain)
            request.set_SubDomain(sub_domain + '.' + domain)
            response = self._client.do_action_with_exception(request)  # 获取域名解析记录列表
            domain_list = json.loads(response)  # 将返回的JSON数据转化为Python能识别的

            ip = urlopen('https://api-ipv4.ip.sb/ip').read()  # 使用IP.SB的接口获取ipv4地址
            ipv4 = str(ip, encoding='utf-8').strip()
            ipv4_out = ipv4
            print("获取到IPv4地址:%s" % ipv4)

            if domain_list['TotalCount'] == 0:
                self.add(domain, sub_domain, "A", ipv4)
                print("新建域名解析成功")
                result = True
            elif domain_list['TotalCount'] == 1:
                if domain_list['DomainRecords']['Record'][0]['Value'].strip() != ipv4.strip():
                    self.update(domain_list['DomainRecords']['Record'][0]['RecordId'], sub_domain, "A", ipv4)
                    result = True
                    print("修改域名解析成功")
                else:  # https://blog.zeruns.tech
                    print("IPv4地址没变")
            elif domain_list['TotalCount'] > 1:
                from aliyunsdkalidns.request.v20150109.DeleteSubDomainRecordsRequest import DeleteSubDomainRecordsRequest
                request = DeleteSubDomainRecordsRequest()
                request.set_accept_format('json')
                request.set_DomainName(domain)  # https://blog.zeruns.tech
                request.set_RR(sub_domain)
                response = self._client.do_action_with_exception(request)
                self.add(domain, sub_domain, "A", ipv4)
                print("修改域名解析成功")
                result = True

        if ipv6_flag == 1:
            request = DescribeSubDomainRecordsRequest()
            request.set_accept_format('json')
            request.set_DomainName(domain)
            request.set_SubDomain(sub_domain + '.' + domain)
            response = self._client.do_action_with_exception(request)  # 获取域名解析记录列表
            domain_list = json.loads(response)  # 将返回的JSON数据转化为Python能识别的

            ip = urlopen('https://api-ipv6.ip.sb/ip').read()  # 使用IP.SB的接口获取ipv6地址
            ipv6 = str(ip, encoding='utf-8').strip()
            print("获取到IPv6地址:%s" % ipv6)

            if domain_list['TotalCount'] == 0:
                self.add(domain, sub_domain, "AAAA", ipv6)
                print("新建域名解析成功")
            elif domain_list['TotalCount'] == 1:
                if domain_list['DomainRecords']['Record'][0]['Value'].strip() != ipv6.strip():
                    self.update(domain_list['DomainRecords']['Record'][0]['RecordId'], sub_domain, "AAAA", ipv6)
                    print("修改域名解析成功")
                else:  # https://blog.zeruns.tech
                    print("IPv6地址没变")
            elif domain_list['TotalCount'] > 1:
                from aliyunsdkalidns.request.v20150109.DeleteSubDomainRecordsRequest import DeleteSubDomainRecordsRequest
                request = DeleteSubDomainRecordsRequest()
                request.set_accept_format('json')
                request.set_DomainName(domain)
                request.set_RR(sub_domain)  # https://blog.zeruns.tech
                response = self._client.do_action_with_exception(request)
                self.add(domain, sub_domain, "AAAA", ipv6)
                print("修改域名解析成功")
        return ipv4_out,result
Ejemplo n.º 15
0
def describe_records(value=True):
    try:
        logging.info('DescribeSubDomainRecordsRequest %s' % SUBDOMAIN)
        request = DescribeSubDomainRecordsRequest()
        request.set_DomainName(DOMAIN)
        request.set_SubDomain(SUBDOMAIN)
        request.set_Type(RECORD_TYPE)
        request.set_Line('default')
        request.set_accept_format('json')
        response = acsClient.do_action_with_exception(request)
        if value:
            return json.loads(response)['DomainRecords']['Record'][0]['Value']
        else:
            return json.loads(
                response)['DomainRecords']['Record'][0]['RecordId']
    except Exception as e:
        logging.error(e)
Ejemplo n.º 16
0
    response = client.do_action_with_exception(request)


def add(DomainName, RR, Type, Value):  # 添加新的域名解析记录
    from aliyunsdkalidns.request.v20150109.AddDomainRecordRequest import AddDomainRecordRequest
    request = AddDomainRecordRequest()
    request.set_accept_format('json')
    request.set_DomainName(DomainName)
    request.set_RR(RR)  # https://blog.zeruns.tech
    request.set_Type(Type)
    request.set_Value(Value)
    response = client.do_action_with_exception(request)


if ipv4_flag == 1:
    request = DescribeSubDomainRecordsRequest()
    request.set_accept_format('json')
    request.set_DomainName(domain)
    request.set_SubDomain(name_ipv4 + '.' + domain)
    response = client.do_action_with_exception(request)  # 获取域名解析记录列表
    domain_list = json.loads(response)  # 将返回的JSON数据转化为Python能识别的

    ip = urlopen('https://api-ipv4.ip.sb/ip').read()  # 使用IP.SB的接口获取ipv4地址
    ipv4 = str(ip, encoding='utf-8')
    print("获取到IPv4地址:%s" % ipv4)

    if domain_list['TotalCount'] == 0:
        add(domain, name_ipv4, "A", ipv4)
        print("新建域名解析成功")
    elif domain_list['TotalCount'] == 1:
        if domain_list['DomainRecords']['Record'][0]['Value'].strip() != ipv4.strip():
def ddns_update(RR):
    print("当前RR值:%s"%(RR))
    if ipv4_flag == 1:
        request = DescribeSubDomainRecordsRequest()
        request.set_accept_format('json')
        request.set_DomainName(domain)
        request.set_SubDomain(RR + '.' + domain)

        response = client.do_action_with_exception(request)  # 获取域名解析记录列表
        domain_list = json.loads(response)  # 将返回的JSON数据转化为Python能识别的

        if domain_list['TotalCount'] == 0:
            add(domain, RR, "A", ipv4)
            print("新建域名解析成功")
        elif domain_list['TotalCount'] == 1:
            if domain_list['DomainRecords']['Record'][0]['Value'].strip() != ipv4.strip():
                update(domain_list['DomainRecords']['Record'][0]['RecordId'], RR, "A", ipv4)
                print("修改域名解析成功")
            else:  
                print("IPv4地址没变")
        elif domain_list['TotalCount'] > 1:
            from aliyunsdkalidns.request.v20150109.DeleteSubDomainRecordsRequest import DeleteSubDomainRecordsRequest
            request = DeleteSubDomainRecordsRequest()
            request.set_accept_format('json')
            request.set_DomainName(domain)  
            request.set_RR(RR)
            response = client.do_action_with_exception(request)
            add(domain, RR, "A", ipv4)
            print("修改域名解析成功")