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

change loop output to per line instead of a group

我正在尝试为我的脚本的其余部分获得所需的输出...目前,当我分配一个名为 "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 上使用长度函数,如果它大于 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])

你必须遍历 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