如何打印路由53 DN的域名

2024-06-03 03:50:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我是python新手,正在学习,我正在编写一个代码来打印域名、route53托管区域的类型和值。 当它遍历CNAME时,我得到CNAME的值,而不是它的域名。

def list(zoneid, region, profile):
  rrs = []
  aws_session = boto3.session.Session(region=region, profile=profile)
  route53 = aws_session.client('route53')
  paginator = route53.get_paginator('list_resource_record_sets')

  page = paginator.paginate(
  HostedZoneId=zoneid,
  PaginationConfig={
      'MaxItems': 500,
      'PageSize': 500
  }
  )



  for i in page:
    for record in i['ResourceRecordSets']:
      if record['Type'] == 'CNAME':
        a.extend(x['Value'] for x in record['ResourceRecords'])
      elif record['Type'] == 'A':
        a.append(record['Name'])

  return a

打印记录[Name]-给出域名。但我如何将它包含在“a.extend(x['Value']for x in record['ResourceRecords'])这一行


Tags: inawsforsessiontypepagerecordprofile
1条回答
网友
1楼 · 发布于 2024-06-03 03:50:33

从示例响应on the docs可以看出,任何资源记录都没有可用的Value键:

{
    'ResourceRecordSets': [
        {
            'Name': 'string',
            'Type': 'SOA'|'A'|'TXT'|'NS'|'CNAME'|'MX'|'NAPTR'|'PTR'|'SRV'|'SPF'|'AAAA'|'CAA',
            'SetIdentifier': 'string',
            'Weight': 123,
            'Region': 'us-east-1'|'us-east-2'|'us-west-1'|'us-west-2'|'ca-central-1'|'eu-west-1'|'eu-west-2'|'eu-west-3'|'eu-central-1'|'ap-southeast-1'|'ap-southeast-2'|'ap-northeast-1'|'ap-northeast-2'|'sa-east-1'|'cn-north-1'|'cn-northwest-1'|'ap-south-1',
            'GeoLocation': {
                'ContinentCode': 'string',
                'CountryCode': 'string',
                'SubdivisionCode': 'string'
            },
            'Failover': 'PRIMARY'|'SECONDARY',
            'MultiValueAnswer': True|False,
            'TTL': 123,
            'ResourceRecords': [
                {
                    'Value': 'string'
                },
            ],
            'AliasTarget': {
                'HostedZoneId': 'string',
                'DNSName': 'string',
                'EvaluateTargetHealth': True|False
            },
            'HealthCheckId': 'string',
            'TrafficPolicyInstanceId': 'string'
        },
    ],
    'IsTruncated': True|False,
    'MaxItems': 'string',
    'NextToken': 'string'
}

我想您只需要引用Name键:

Name (string)

The name of the domain you want to perform the action on.

Enter a fully qualified domain name, for example, www.example.com. You can optionally include a trailing dot. If you omit the trailing dot, Amazon Route 53 still assumes that the domain name that you specify is fully qualified. This means that Amazon Route 53 treats www.example.com (without a trailing dot) and www.example.com. (with a trailing dot) as identical.

相关问题 更多 >