简体   繁体   English

Boto3访问对象

[英]Boto3 accessing Objects

I am new to boto3 and trying to access some items that are in an object: 我是boto3的新手,正在尝试访问对象中的某些项目:

acl = ec2_client.describe_network_acls( Filters=[{'Name':'tag:demo','Values': ['true']}] )

This returns the correctly tagged ACL from AWS and stores the object in "acl". 这将从AWS返回正确标记的ACL,并将对象存储在“ acl”中。 Now I would like to get some items out of the object. 现在,我想从对象中取出一些物品。 I need to be able to find the inbound and outbound rules numbered 100 and 105 from the data set below: 我需要能够从以下数据集中找到编号为100和105的入站和出站规则:

{
u 'NetworkAcls': [{
    u 'Associations': [],
    u 'NetworkAclId': 'acl-xxxxxxxx',
    u 'VpcId': 'vpc-xxxxxxx',
    u 'Tags': [{
        u 'Value': 'test-demo-dev',
        u 'Key': 'Name'
    }, {
        u 'Value': 'dev',
        u 'Key': 'env'
    }, {
        u 'Value': 'true',
        u 'Key': 'demo'
    }],
    u 'Entries': [{
        u 'RuleNumber': 100,
        u 'Protocol': '6',
        u 'PortRange': {
            u 'To': 443,
            u 'From': 443
        },
        u 'Egress': True,
        u 'RuleAction': 'allow',
        u 'CidrBlock': '34.x.x.x/32'
    }, {
        u 'RuleNumber': 105,
        u 'Protocol': '6',
        u 'PortRange': {
            u 'To': 443,
            u 'From': 443
        },
        u 'Egress': True,
        u 'RuleAction': 'allow',
        u 'CidrBlock': '54.x.x.x/32'
    }, {
        u 'RuleNumber': 110,
        u 'Protocol': '6',
        u 'PortRange': {
            u 'To': 65535,
            u 'From': 1024
        },
        u 'Egress': True,
        u 'RuleAction': 'allow',
        u 'CidrBlock': '10.x.x.x/x'
    }, {
        u 'RuleNumber': 115,
        u 'Protocol': '6',
        u 'PortRange': {
            u 'To': 65535,
            u 'From': 1024
        },
        u 'Egress': True,
        u 'RuleAction': 'allow',
        u 'CidrBlock': '10.x.x.x/x'
    }, {
        u 'RuleNumber': 120,
        u 'Protocol': '6',
        u 'PortRange': {
            u 'To': 65535,
            u 'From': 1024
        },
        u 'Egress': True,
        u 'RuleAction': 'allow',
        u 'CidrBlock': '10.x.x.x/x'
    }, {
        u 'RuleNumber': 32767,
        u 'Protocol': '-1',
        u 'Egress': True,
        u 'CidrBlock': '0.0.0.0/0',
        u 'RuleAction': 'deny'
    }, {
        u 'RuleNumber': 100,
        u 'Protocol': '6',
        u 'PortRange': {
            u 'To': 65535,
            u 'From': 1024
        },
        u 'Egress': False,
        u 'RuleAction': 'allow',
        u 'CidrBlock': '34.x.x.x/32'
    }, {
        u 'RuleNumber': 105,
        u 'Protocol': '6',
        u 'PortRange': {
            u 'To': 65535,
            u 'From': 1024
        },
        u 'Egress': False,
        u 'RuleAction': 'allow',
        u 'CidrBlock': '54.x.x.x/32'
    }, {
        u 'RuleNumber': 110,
        u 'Protocol': '6',
        u 'PortRange': {
            u 'To': 443,
            u 'From': 443
        },
        u 'Egress': False,
        u 'RuleAction': 'allow',
        u 'CidrBlock': '10.x.x.x/x'
    }, {
        u 'RuleNumber': 115,
        u 'Protocol': '6',
        u 'PortRange': {
            u 'To': 443,
            u 'From': 443
        },
        u 'Egress': False,
        u 'RuleAction': 'allow',
        u 'CidrBlock': '10.x.x.x/x'
    }, {
        u 'RuleNumber': 120,
        u 'Protocol': '6',
        u 'PortRange': {
            u 'To': 443,
            u 'From': 443
        },
        u 'Egress': False,
        u 'RuleAction': 'allow',
        u 'CidrBlock': '10.x.x.x/x'
    }, {
        u 'RuleNumber': 32767,
        u 'Protocol': '-1',
        u 'Egress': False,
        u 'CidrBlock': '0.0.0.0/0',
        u 'RuleAction': 'deny'
    }],
    u 'IsDefault': False
}], 'ResponseMetadata': {
    'RetryAttempts': 0,
    'HTTPStatusCode': 200,
    'RequestId': '37258db0-6b9e-4a20-96f3-dc3bc5214b52',
    'HTTPHeaders': {
        'transfer-encoding': 'chunked',
        'vary': 'Accept-Encoding',
        'server': 'AmazonEC2',
        'content-type': 'text/xml;charset=UTF-8',
        'date': 'Tue, 20 Jun 2017 11:22:47 GMT'
    }
}

} }

In particular I would like to be able to find out the 'CidrBlock' value for inbound and outbound rules 100 and 105. 特别是,我希望能够找到入站和出站规则100和105的'CidrBlock'值。

Can anyone help? 有人可以帮忙吗? I am sure once I see it working for the first time I can go on and apply to other scenarios but just can't seem to get my head around it for the first time. 我确信一旦我第一次看到它可以工作,就可以继续适用于其他场景,但是似乎无法第一次绕开它。

I guess I need to loop over parts of the object (Entries) and if 'RuleNumber' 100 or 105 and i need to store ingress and egress separately but where to start..... 我想我需要循环遍历对象的某些部分(条目),如果“ RuleNumber”为100或105,并且我需要分别存储入口和出口,但从何处开始.....

The data you have mentioned contains a unicode string keys and you have accidently added an extra space while asking question . 您提到的数据包含unicode字符串键,并且您在提问时不小心添加了额外的空间。

For eg - 例如-

 InCorrect - u 'NetworkAcls'
 Correct - u'NetworkAcls'

For getting CIDR Block of rules 100 and 105 from the data you mentioned above, you can get it like this - 为了从您上面提到的数据中获取CIDR规则100和105块,您可以这样获得-

   NetworkAcls = acl['NetworkAcls']
   for netacl in NetworkAcls:
      entries = netacl['Entries']
      for entry in entries:
         if entry.get('RuleNumber') == 100 or entry.get('RuleNumber') == 105 :
            print(entry.get('CidrBlock' ,None))

Output 输出量

 34.x.x.x/32
 54.x.x.x/32
 34.x.x.x/32
 54.x.x.x/32

Is that what you are looking for ? 那是您要找的东西吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM