限制嵌套for循环输出

limit nested for loop output

我是 运行 下面的代码,用于获取安全组列表,我得到了预期的行为,但我正在尝试查看是否有办法制作此代码,以便仅输出输出入站规则一次与源......因此对于名为 "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]
>>> 

我确定这不是最好的方法,但这是我想出的...这几乎和我正在寻找的一样

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

尝试这样的事情:

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