在整个循环结束时打印最终结果

时间:2016-04-22 06:32:35

标签: python

代码块看起来像

regions = ['us-east-1','us-west-1','us-west-2','eu-west-1','sa-east-1','ap-southeast-1','ap-southeast-2','ap-northeast-1']

data = []
header = ["Port","Open For","Security group","Instance Details"]

    for region in regions:
        connection=ec2.connect_to_region(region)
        sg = connection.get_all_security_groups()
        try:
            for securityGroup in sg:
               for rule in securityGroup.rules:
                   if '0.0.0.0/0' in str(rule.grants):
                      for instanceid in securityGroup.instances():
                          instanceId=str(instanceid)
                         # print instanceId
                          tag = getTag(connection, instanceId.split(':')[1])
                          if tag is not None:
                            # print tab              
                               tab =[str(rule.to_port),"0.0.0.0/0",str(securityGroup.name),str(getTag(connection, instanceId.split(':')[1]))]
                               data.append(tab)
            print tabulate(data, headers=header, tablefmt='simple')

这会产生以下结果:

  Port  Open For    Security group           Instance Details
------  ----------  -----------------------  ------------------------------------------------------------------------
 65535  0.0.0.0/0   launch-wizard-mingjun    (u'testVM10001', u'192.168.0.93', RegionInfo:us-east-1)
    22  0.0.0.0/0   launch-wizard-mingjun    (u'testVM10001', u'192.168.0.93', RegionInfo:us-east-1)
    80  0.0.0.0/0   SSH+HTTPS                (u'security_monkey_production', u'172.30.2.31', RegionInfo:us-east-1)
    22  0.0.0.0/0   SSH+HTTPS                (u'security_monkey_production', u'172.30.2.31', RegionInfo:us-east-1)
   443  0.0.0.0/0   SSH+HTTPS                (u'security_monkey_production', u'172.30.2.31', RegionInfo:us-east-1)
    22  0.0.0.0/0   na-prod-1w-secgroup      (u'na-prod-1w-scc-94a54e56', u'10.197.51.19', RegionInfo:us-west-1)

在这里待一段时间然后打印

  Port  Open For    Security group           Instance Details
------  ----------  -----------------------  ------------------------------------------------------------------------
 65535  0.0.0.0/0   launch-wizard-mingjun    (u'testVM10001', u'192.168.0.93', RegionInfo:us-east-1)
    22  0.0.0.0/0   launch-wizard-mingjun    (u'testVM10001', u'192.168.0.93', RegionInfo:us-east-1)
    80  0.0.0.0/0   SSH+HTTPS                (u'security_monkey_production', u'172.30.2.31', RegionInfo:us-east-1)
    22  0.0.0.0/0   SSH+HTTPS                (u'security_monkey_production', u'172.30.2.31', RegionInfo:us-east-1)
   443  0.0.0.0/0   SSH+HTTPS                (u'security_monkey_production', u'172.30.2.31', RegionInfo:us-east-1)
    22  0.0.0.0/0   na-prod-1w-secgroup      (u'na-prod-1w-scc-94a54e56', u'10.197.51.19', RegionInfo:us-west-1)
   443  0.0.0.0/0   na-prod-1w-secgroup      (u'na-prod-1w-scc-94a54e56', u'10.197.51.19', RegionInfo:us-west-1)
    80  0.0.0.0/0   sfo01-devops-haproxy-sg  (u'sfo01-cwptest-LBi-f207da47', u'192.168.80.171', RegionInfo:us-west-1)
    80  0.0.0.0/0   sfo01-devops-haproxy-sg  (u'sfo01-cwptest-LBi-e1a72422', u'192.168.80.156', RegionInfo:us-west-1)
   443  0.0.0.0/0   sfo01-devops-haproxy-sg  (u'sfo01-cwptest-LBi-f207da47', u'192.168.80.171', RegionInfo:us-west-1)
   443  0.0.0.0/0   sfo01-devops-haproxy-sg  (u'sfo01-cwptest-LBi-e1a72422', u'192.168.80.156', RegionInfo:us-west-1)
 65535  0.0.0.0/0   Nishant-Open             (u'POC-demo-es/kib', u'10.205.11.150', RegionInfo:us-west-2)
    22  0.0.0.0/0   Nishant-Open             (u'POC-demo-es/kib', u'10.205.11.150', RegionInfo:us-west-2)
  5601  0.0.0.0/0   Nishant-Open  
....

为什么不一气呵成?

2 个答案:

答案 0 :(得分:1)

在循环开始之前你可以做什么创建名为dataheader

的列表变量
data = []
header = ["Port","Open For","Security group","Instance Details"]
for region in regions:
    # your code....
    .
    .
    .
    if tag is not None:
        tab =[str(rule.to_port),"0.0.0.0/0",str(securityGroup.name),str(getTag(connection, instanceId.split(':')[1]))]    
        data.append(tab)
print tabulate(data, headers=header, tablefmt='grid')

答案 1 :(得分:0)

将标签的值存储在字典中甚至列表中,并在程序结束时打印字典。您可以使用打印格式以表格形式打印。