【发布时间】:2015-07-22 05:39:06
【问题描述】:
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
SecurityGroup:default sg-e1304484 inbound: IPPermissions:tcp(80-80) source: [67.184.225.222/32]
SecurityGroup:default sg-e1304484 inbound: IPPermissions:tcp(5500-5500) source: [67.184.225.222/32]
SecurityGroup:Pub_HDP_SG sg-e632d982 inbound: IPPermissions:tcp(80-80) source: [0.0.0.0/0]
SecurityGroup:sg3-MySecurityGroup-LB0QF9UQAOEF sg-4fe73728 inbound: IPPermissions:tcp(22-22) source: [0.0.0.0/0]
SecurityGroup:sg3-MySecurityGroup-LB0QF9UQAOEF sg-4fe73728 inbound: IPPermissions:tcp(80-80) 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) source: [10.0.20.100/32]
SecurityGroup:us-east-open-all sg-97ffa7f2 inbound: IPPermissions:tcp(53-53) source: [10.0.20.100/32]
SecurityGroup:wordpress-app-SG sg-99c4befc inbound: IPPermissions:-1(None-None) source: [sg-e632d982-995635159130]
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(1024-65535) source: [10.0.20.100/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(138-138) source: [10.0.20.100/32]
SecurityGroup:wordpress-app-SG sg-99c4befc inbound: IPPermissions:udp(53-53) source: [24.12.30.198/32]
SecurityGroup:wordpress-app-SG sg-99c4befc inbound: IPPermissions:tcp(30015-30015) source: [0.0.0.0/0]
SecurityGroup:wordpress-app-SG sg-99c4befc inbound: IPPermissions:icmp(-1--1) source: [10.0.20.100/32]
SecurityGroup:default sg-c65a20a3 inbound: IPPermissions:-1(None-None) source: [sg-c65a20a3-995635159130]
SecurityGroup:default sg-c65a20a3 inbound: IPPermissions:-1(None-None) source: [sg-99c4befc-995635159130]
SecurityGroup:sg3-MySecurityGroup2-1HGPN4UF57XN6 sg-4ee73729 inbound: IPPermissions:tcp(22-22) source: [192.168.1.12/32]
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) source: [10.0.20.100/32]
SecurityGroup:launch-wizard-2 sg-932255f6 inbound: IPPermissions:tcp(443-443) source: [0.0.0.0/0]
>>>
大家好,
对于每个安全组,我如何将其转换为一个列表,该列表又将包含 cidr 块、协议类型和端口的字典...所以从上面的输出来看,名为“默认”的安全组有 2 条规则。 ..允许从 80 和 5500 到源 IP 的 TCP 端口,然后称为“Pub_HDP_SG”的安全组只有一个规则……依此类推……这是我试图以表格形式输出的输出一个列表......
我打算做的是,获取列表(和嵌套字典)并将其传递给一个函数,该函数将使用对流层(类似于“http://imil.net/wp/2015/06/04/rock-your-cloudformation-with-troposphere-and-boto/”)输出一个 cloudformation 模板
rule1 = [{
'cidr': '67.184.225.222/32',
'proto': 'tcp',
'port': 80
},{
'cidr': '67.184.225.222/32',
'proto': 'tcp',
'port': 5500
}]
rule2 = [{
'cidr': '[0.0.0.0/0',
'proto': 'tcp',
'port': 80
}]
rule3 = [{
'cidr': '0.0.0.0/0',
'proto': 'tcp',
'port': 22
},{
'cidr': '0.0.0.0/0',
'proto': 'tcp',
'port': 80
}]
【问题讨论】:
标签: python python-2.7 python-3.x amazon-web-services boto