将循环输出更改为每行而不是组

时间:2015-07-30 19:28:30

标签: python python-2.7 python-3.x amazon-web-services boto

我正在尝试为我的脚本的其余部分获得所需的输出...当前当我指定一个名为“st”的变量时,我得到以下输出...但请注意其中一行我得到了cidr区块“[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]”我怎么能打破这个,所以我得到一个期望的输出(看看最后的结果)....

我希望最终输出如下所示...注意大CIDR块是如何分解的,所以它现在是5行而不是1行

......
......
......
(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])
......
......
......

所以我想也许我可以在rules.grants上使用length函数,如果它大于1,那么构建一个diff“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
>>> 

有关如何实现这一点的想法吗?

我尝试了@helloV曾经问过的问题,而且似乎已经解决了这个问题......但请注意,这些线路(附加线路)的cidr块现在没有列表括号.....对于2.2的线路。 2.2 / 32,12.12.12.12 / 32,13.13.13.13 / 32,14.14.14.14 / 32

(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)
(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])
(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])

1 个答案:

答案 0 :(得分:1)

你必须遍历rule.grants

for rule in sg.rules:
  if len(rule.grants) > 1:
    for grant in rule.grants:
      st = sg, sg.id, "inbound:", rule, " source:", [grant]
      print st
  else:
      st = sg, sg.id, "inbound:", rule, " source:", rule.grants
      print st