Python ';非类型';对象没有属性';获取所有安全组';

Python ';非类型';对象没有属性';获取所有安全组';,python,amazon-web-services,boto,Python,Amazon Web Services,Boto,我被困在这里了 . regions = ['us-west-2','eu-central-1','ap-southeast-1'] ##Prod VPC data = [] header = ["Port","Open For","Security group","Instance Details"] for region in regions: connection=ec2.connect_to_region(region) print connection

我被困在这里了

    .
regions = ['us-west-2','eu-central-1','ap-southeast-1']  ##Prod VPC
data = []
header = ["Port","Open For","Security group","Instance Details"]

for region in regions:
    connection=ec2.connect_to_region(region)
    print connection
    sg=connection.get_all_security_groups()
    print sg
    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)
    except Exception,e:
        pass
这将导致以下结果

    EC2Connection:ec2.us-west-2.amazonaws.com
    [SecurityGroup: too many sec groups here.........]
None
Traceback (most recent call last):
  File "aws_sec_scan.py", line 29, in <module>
    sg=connection.get_all_security_groups()
AttributeError: 'NoneType' object has no attribute 'get_all_security_groups'
ec2连接:ec2.us-west-2.amazonaws.com
[安全组:此处的sec组太多……]
没有一个
回溯(最近一次呼叫最后一次):
文件“aws_secu_scan.py”,第29行,在
sg=连接。获取所有安全组()
AttributeError:“非类型”对象没有“获取所有安全组”属性

为什么我会得到“没有”

这是因为连接为None,而您正试图从None值调用函数

sg=connection.get_all_security_groups()

因此,请检查连接是否正确。

这是因为连接为“无”,并且您正试图从“无”值调用函数

sg=connection.get_all_security_groups()

因此,请检查连接是否正确。

中断堆栈跟踪:

在第一次迭代中,您有
连接
,因此没有错误:

EC2Connection:ec2.us-west-2.amazonaws.com # this is your connection
[SecurityGroup: too many sec groups here.........] # this being the security groups
在第二次迭代中:

因此,现在如果使用
sg=connection.get\u all\u security\u groups()
则会出现以下错误,因为
连接是
None

Traceback (most recent call last):
  File "aws_sec_scan.py", line 29, in <module>
    sg=connection.get_all_security_groups()
AttributeError: 'NoneType' object has no attribute 'get_all_security_groups'
回溯(最近一次呼叫最后一次):
文件“aws_secu_scan.py”,第29行,在
sg=连接。获取所有安全组()
AttributeError:“非类型”对象没有“获取所有安全组”属性

分解堆栈跟踪:

在第一次迭代中,您有
连接
,因此没有错误:

EC2Connection:ec2.us-west-2.amazonaws.com # this is your connection
[SecurityGroup: too many sec groups here.........] # this being the security groups
在第二次迭代中:

因此,现在如果使用
sg=connection.get\u all\u security\u groups()
则会出现以下错误,因为
连接是
None

Traceback (most recent call last):
  File "aws_sec_scan.py", line 29, in <module>
    sg=connection.get_all_security_groups()
AttributeError: 'NoneType' object has no attribute 'get_all_security_groups'
回溯(最近一次呼叫最后一次):
文件“aws_secu_scan.py”,第29行,在
sg=连接。获取所有安全组()
AttributeError:“非类型”对象没有“获取所有安全组”属性


什么是
ec2。连接到区域(区域)
返回?您的连接没有。从日志中可以明显看出。在循环的第一次迭代中,您似乎拥有
连接,但在第二次迭代中丢失了它。@ReblochonMasque ec2.us-west-2.amazonaws。com@Nishant请检查我的答案,您仅在第一次迭代中获得上述值,而不是在第二次迭代中。什么是
ec2.connect\u to\u region(区域)
返回?您的连接没有。从日志中可以明显看出。在循环的第一次迭代中,您似乎有
连接,但在第二次迭代中丢失了它。@ReblochonMasque ec2.us-west-2.amazonaws。com@Nishant请检查我的答案,您仅在第一次迭代中获得上述值,而不是在第二次迭代中ne.好的,我已经更新了代码,我不确定如何不中断连接。它有意义吗?为什么连接为“无”?我不明白它没有意义,但你可以试试
ec2。将\u连接到\u区域(区域)
在控制台中,用于您没有得到任何信息的特定区域。
>>从boto导入ec2>>>ec2。将\u连接到\u区域(“us-west-1”)EC2Connection:ec2.us-west-1.amazonaws.com
谢谢你,伙计。我刚刚发现同一组命令在我的本地虚拟机上运行得很好,这只适用于AWS机器……看看我发现了什么。我已经更新了代码,我不确定如何不中断连接。这有意义吗?为什么连接是“无”?我想不出这是没有意义的,但您可以尝试一下
ec2。在控制台中,将\u连接到\u区域(区域)
,用于您没有得到的特定区域。
>>从boto导入ec2>>>ec2。将\u连接到\u区域(“us-west-1”)EC2Connection:ec2.us-west-1.amazonaws.com
谢谢你,伙计。我刚刚发现同一组命令在我的本地虚拟机上运行得非常完美,这只适用于AWS机器……我要看看我发现了什么有意义吗?为什么连接为“无”?我不明白它有意义吗?为什么连接为“无”?我不明白