限制嵌套的循环输出

时间:2015-07-19 21:01:56

标签: python amazon-web-services boto aws-sdk

我正在运行下面的代码来获取安全组列表,我得到了预期的行为,但我试图看看是否有办法制作此代码,以便输出仅使用源端口输出一次入站规则...所以对于名为" wordpress-app-SG"输出看起来像下面

SecurityGroup:wordpress-app-SG sg-99c4befc inbound: [IPPermissions:tcp(22-22)]  source: [67.184.225.222/32]
SecurityGroup:wordpress-app-SG sg-99c4befc inbound: [IPPermissions:tcp(80-80)]  source: [24.12.30.198/32]
SecurityGroup:wordpress-app-SG sg-99c4befc inbound: [IPPermissions:udp(53-53)]  source: [24.12.30.198/32]

到目前为止,我正在与之合作......

SecurityGroup:default sg-e1304484 inbound: [IPPermissions:-1(None-None), IPPermissions:tcp(5500-5500)]  source: [67.184.225.222/32]
SecurityGroup:default sg-e1304484 inbound: [IPPermissions:-1(None-None), IPPermissions:tcp(5500-5500)]  source: [67.184.225.222/32]
SecurityGroup:Pub_HDP_SG sg-e632d982 inbound: [IPPermissions:-1(None-None)]  source: [0.0.0.0/0]
SecurityGroup:RDP Rule - open everyone  sg-42d58d27 inbound: [IPPermissions:-1(None-None)]  source: [0.0.0.0/0]
SecurityGroup:us-east-open-all sg-97ffa7f2 inbound: [IPPermissions:tcp(22-22), IPPermissions:tcp(53-53)]  source: [10.0.1.2/32]
SecurityGroup:us-east-open-all sg-97ffa7f2 inbound: [IPPermissions:tcp(22-22), IPPermissions:tcp(53-53)]  source: [10.0.1.2/32]
SecurityGroup:wordpress-app-SG sg-99c4befc inbound: [IPPermissions:tcp(22-22), IPPermissions:tcp(80-80), IPPermissions:udp(53-53)]  source: [67.184.225.222/32]
SecurityGroup:wordpress-app-SG sg-99c4befc inbound: [IPPermissions:tcp(22-22), IPPermissions:tcp(80-80), IPPermissions:udp(53-53)]  source: [24.12.30.198/32]
SecurityGroup:wordpress-app-SG sg-99c4befc inbound: [IPPermissions:tcp(22-22), IPPermissions:tcp(80-80), IPPermissions:udp(53-53)]  source: [24.12.30.198/32]
SecurityGroup:default sg-c65a20a3 inbound: [IPPermissions:-1(None-None), IPPermissions:-1(None-None)]  source: [sg-c65a20a3-995635159130]
SecurityGroup:default sg-c65a20a3 inbound: [IPPermissions:-1(None-None), IPPermissions:-1(None-None)]  source: [sg-99c4befc-995635159130]
SecurityGroup:AWS-AMI-SG sg-35568d51 inbound: [IPPermissions:tcp(22-22)]  source: [0.0.0.0/0]
SecurityGroup:launch-wizard-2 sg-932255f6 inbound: [IPPermissions:tcp(22-22), IPPermissions:tcp(443-443)]  source: [10.0.1.2/32]
SecurityGroup:launch-wizard-2 sg-932255f6 inbound: [IPPermissions:tcp(22-22), IPPermissions:tcp(443-443)]  source: [0.0.0.0/0]
>>> 

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

import boto.ec2

sgs = boto.ec2.connect_to_region('us-east-1').get_all_security_groups()

for sg in sgs:
    for rule in sg.rules:
        print sg, sg.id, "inbound:", rule, " source:", rule.grants

答案 1 :(得分:0)

我肯定不是最好的方法,但这就是我想出来的......这几乎与我正在寻找的东西相同

for i in range(ln):
    sg = ec2.get_all_security_groups()[i]
    ru_ln = len(ec2.get_all_security_groups()[i].rules)
    print "\n"
    print "######################", sg, sg.id, "######################"
    print "\n"
    for g in range(ru_ln):
        so = ec2.get_all_security_groups()[i].rules[g].grants
        sg2 = ec2.get_all_security_groups()[i].rules[g]
        print "inbound:", sg2, "source:", so