I am trying to get a desired output for rest of my script to work...currently when i assign a varaible called "st", i get the below output...but note that one of the lines i get a cidr block of "[2.2.2.2/32, 12.12.12.12/32, 13.13.13.13/32, 14.14.14.14/32, 15.15.15.15/32]"....how can i break this down so i get a desired output(look at very end for this)....
>>> import boto.ec2
>>> fts = {'vpc-id': 'vpc-1895327d', 'group-name': 'Full blown SG test'}
>>> sgs = boto.ec2.connect_to_region("us-east-1", aws_access_key_id='XXXXXXXX', aws_secret_access_key='XXXXXX').get_all_security_groups(filters=fts)
>>> for sg in sgs:
for rule in sg.rules:
st = sg, sg.id, "inbound:", rule, " source:", rule.grants
print st
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [sg-c65a20a3-995635159130])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [sg-99c4befc-995635159130])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(110-110), ' source:', [9.9.9.9/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(0-443), ' source:', [4.4.4.4/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(443-443), ' source:', [0.0.0.0/0])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:icmp(-1--1), ' source:', [3.3.3.3/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(3306-3306), ' source:', [5.5.5.5/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [sg-35568d51-995635159130])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(0-65535), ' source:', [1.1.1.1/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(389-389), ' source:', [10.10.10.10/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [2.2.2.2/32, 12.12.12.12/32, 13.13.13.13/32, 14.14.14.14/32, 15.15.15.15/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:udp(53-53), ' source:', [7.7.7.7/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(25-25), ' source:', [11.11.11.11/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(53-53), ' source:', [8.8.8.8/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(5439-5439), ' source:', [6.6.6.6/32])
>>>
>>>
>>>
i want the final output to be something like below...note how the big CIDR block is broken down so now it is on 5 lines instead of 1 line
......
......
......
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(389-389), ' source:', [10.10.10.10/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [2.2.2.2/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [12.12.12.12/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [13.13.13.13/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [14.14.14.14/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [15.15.15.15/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:udp(53-53), ' source:', [7.7.7.7/32])
......
......
......
So i thought maybe i can use the length function on rules.grants and if its is greater than 1 then build a diff "st" variable.
>>> for sg in sgs:
for rule in sg.rules:
if len(rule.grants) > 1:
st = sg, sg.id, "inbound:", rule, " source:", rule.grants[sg]
else:
st = sg, sg.id, "inbound:", rule, " source:", rule.grants
print st
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [sg-c65a20a3-995635159130])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [sg-99c4befc-995635159130])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(110-110), ' source:', [9.9.9.9/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(0-443), ' source:', [4.4.4.4/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(443-443), ' source:', [0.0.0.0/0])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:icmp(-1--1), ' source:', [3.3.3.3/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(3306-3306), ' source:', [5.5.5.5/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:-1(None-None), ' source:', [sg-35568d51-995635159130])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(0-65535), ' source:', [1.1.1.1/32])
(SecurityGroup:Full blown SG test, u'sg-3ff65858', 'inbound:', IPPermissions:tcp(389-389), ' source:', [10.10.10.10/32])
Traceback (most recent call last):
File "<pyshell#206>", line 4, in <module>
st = sg, sg.id, "inbound:", rule, " source:", rule.grants[sg]
TypeError: list indices must be integers, not SecurityGroup
>>>
Any thought on how i can achive this ?
Aucun commentaire:
Enregistrer un commentaire