如何格式化route53-list-records的输出以显示特定信息?

时间:2017-12-11 10:49:20

标签: python python-2.7 amazon-web-services boto3 amazon-route53

我想编写一个Python代码,它使用boto3来查询route53并获取Ip和“服务器角色”,它是每个服务器主机名的一部分。

我是Python的新手,我在格式化输出时遇到了问题。

我目前的代码如下:

#!/usr/bin/python
import boto3

region = 'eu-west-1'
hostedzoneid = 'XXXXXXXX'
stackvpcid = 'vpc-xxxxxxx'

r53client = boto3.client('route53')

response = r53client.list_resource_record_sets(
        HostedZoneId=hostedzoneid,
        StartRecordName='dev',
        StartRecordType='A'
        )

#dict = {}
for resource in response['ResourceRecordSets']:
    print(resource)

输出如下:

{u'ResourceRecords': [{u'Value': 'ns-512.awsdns-00.net.'}, {u'Value': 'ns-1536.awsdns-00.co.uk.'}, {u'Value': 'ns-0.awsdns-00.com.'}, {u'Value': 'ns-1024.awsdns-00.org.'}], u'Type': 'NS', u'Name': 'vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 172800}
{u'ResourceRecords': [{u'Value': 'ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400'}], u'Type': 'SOA', u'Name': 'vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 900}
{u'ResourceRecords': [{u'Value': '172.58.38.137'}], u'Type': 'A', u'Name': 'dev-analytics1.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.17.224'}], u'Type': 'A', u'Name': 'dev-analytics2.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.32.16'}], u'Type': 'A', u'Name': 'dev-api1.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.26.94'}], u'Type': 'A', u'Name': 'dev-api2.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.14.88'}], u'Type': 'A', u'Name': 'dev-api4.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '52.154.154.22'}], u'Type': 'A', u'Name': 'dev-bastion.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.41.124'}], u'Type': 'A', u'Name': 'dev-batch1.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.39.163'}], u'Type': 'A', u'Name': 'dev-cassandra1.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.14.5'}], u'Type': 'A', u'Name': 'dev-cassandra2.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.31.112'}], u'Type': 'A', u'Name': 'dev-cassandra3.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.38.137'}], u'Type': 'A', u'Name': 'dev-kafka-10.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.41.124'}], u'Type': 'A', u'Name': 'dev-kafka-20.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.32.16'}], u'Type': 'A', u'Name': 'dev-kafka-30.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': '172.58.26.94'}], u'Type': 'A', u'Name': 'dev-kafka-31.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}
{u'ResourceRecords': [{u'Value': 'dev-mysql-14-vpc-XXXXXXXX.c1te7xcziik1.eu-west-1.rds.amazonaws.com'}], u'Type': 'CNAME', u'Name': 'dev-mysql.vpc-XXXXXXXX.eu-west-1.company.private.', u'TTL': 300}

所以现在我试图从输出中删除包含单词“kafka”,CNAME和NS记录的所有行,我不明白为什么它们首先显示为“我”我们在“list_resource_record_sets()”中配置了一个过滤器。

然后,在每个主机名中,例如:

Name': 'dev-cassandra3.vpc-XXXXXXXX.eu-west-1.company.private.'

服务器角色将是字符串“cassandra”或其他服务器:analytics,api,bastion等......而Ip将是“Value”。

所以我希望代码能够创建一个看起来像这样的文本列表:

172.58.31.112 : cassandra
52.154.154.22 : bastion

怎么做?我认为我的问题是我不知道如何访问“值”,例如......

1 个答案:

答案 0 :(得分:1)

response['ResourceRecordSets']是ResourceRecords列表。每个ResourceRecords都是一本字典。以下应该可以使用,或者您可以根据需要进行调整。循环遍历记录并检查名称是否具有模式dev-([a-z]*)并打印名称和值。

import re

for resource in response['ResourceRecordSets']
  records = resource['ResourceRecords'][0]
  if 'Name' in resource:
    name = re.findall('dev-([a-z]*)', resource['Name'])
    if name:
      print records['Value'], name[0]

输出

172.58.38.137 analytics
172.58.17.224 analytics
172.58.32.16 api
172.58.26.94 api
172.58.14.88 api
52.154.154.22 bastion
172.58.41.124 batch
172.58.39.163 cassandra
172.58.14.5 cassandra
172.58.31.112 cassandra
172.58.38.137 kafka
172.58.41.124 kafka
172.58.32.16 kafka
172.58.26.94 kafka
dev-mysql-14-vpc-XXXXXXXX.c1te7xcziik1.eu-west-1.rds.amazonaws.com mysql