python - convert output to list(and nested dictionary) -


import boto.ec2  sgs = boto.ec2.connect_to_region('us-east-1').get_all_security_groups()  sg in sgs:     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  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] >>>  

hello all,

for each securitygroup, how can convert list in turn have dictionary of cidr block, protocol type , port...so output above, securitygroup called "default" had 2 rules...allowing tcp port 80 , 5500 source ip , securitygroup called "pub_hdp_sg" had 1 rule...so on , forth....here output trying out in form of list....

what planning is, take list(and nested dictionary) , pass function in turn spitout cloudformation template using troposphere (something "http://imil.net/wp/2015/06/04/rock-your-cloudformation-with-troposphere-and-boto/")

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 }] 

get_all_security_groups() returns list of objects. , each object contains other object, need understand heiracrvhy first. once got can iterate through result , construct dictionary.

for example:

[   {      'cidr': x.rules[0].grants[0],       'proto': x.rules[0].ip_protocol      # on ....   } x in sgs if x.rules ] 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -