Python 3.x 允许0.0.0.0/0的所有SGs列表-使用Python的AWS Lambda

Python 3.x 允许0.0.0.0/0的所有SGs列表-使用Python的AWS Lambda,python-3.x,aws-lambda,Python 3.x,Aws Lambda,我对lambda函数和python比较陌生。尝试实现lambda函数,该函数列出了我的AWS帐户中的所有安全组,它们允许0.0.0.0/0。如果有人能帮忙,我将不胜感激 我尝试过这样做,但它给出了实例,即开放到0.0.0.0/0,相反,我需要一个包含该规则的所有sg的列表 import sys import boto from boto import ec2 from boto import sns connection=ec2.connect_to_region("region-name")

我对lambda函数和python比较陌生。尝试实现lambda函数,该函数列出了我的AWS帐户中的所有安全组,它们允许0.0.0.0/0。如果有人能帮忙,我将不胜感激

我尝试过这样做,但它给出了实例,即开放到0.0.0.0/0,相反,我需要一个包含该规则的所有sg的列表

import sys
import boto
from boto import ec2
from boto import sns
connection=ec2.connect_to_region("region-name")
connSNS = boto.sns.connect_to_region("region-name")
sg=connection.get_all_security_groups()

listOfInstances=""
messages="Following Instances have port open to all"

def getTag(instanceId):
    reservations=connection.get_all_instances(filters={'instance_id':instanceId})
    for r in reservations:
        for i in r.instances:
            return i.tags['Name']

try:
    for securityGroup in sg:
        for rule in securityGroup.rules:
            global instanceId;
            if (rule.from_port=='0' and rule.to_port == '65535') and '0.0.0.0/0' in str(rule.grants):
                for instanceid in securityGroup.instances():
                    instanceId=str(instanceid)
                    listOfInstances += "Instance Name : " + getTag(instanceId.split(':')[1]) + "\t State:" + instanceid.state + "\t SecurityGroup:" +securityGroup.name + "\n"
                    connSNS.publish(topic='SNS-topic-arn-endpoint',message = messages + "\n" + listOfInstances, subject='ProjectName : Server List with Port Open to all')

except :
    print 'Some Error occurred : '
    print sys.exc_info()
    connSNS.publish(topic='SNS-topic-arn-endpoint',message = sys.exc_info(), subject='script ended with error')

这些行专门查找给定安全组的实例:

            for instanceid in securityGroup.instances():
                instanceId=str(instanceid)
                listOfInstances += "Instance Name : " + getTag(instanceId.split(':')[1]) + "\t State:" + instanceid.state + "\t SecurityGroup:" +securityGroup.name + "\n"

如果您不需要实例,请删除这些行,而是返回安全组本身。

旁注:现在,您应该使用而不是
boto