BOTO3查找未使用的安全组



我正在尝试在boto3脚本上了解更多。

我想搜索几个VPC中的未使用的安全组,这些组都在同一区域

我试图将Python脚本在这里工作:boto3搜索未使用的安全组

所以我的list-unused-sq.py如下所示

import boto3
ec2 = boto3.resource('ec2')
sgs = list(ec2.security_groups.all())
insts = list(ec2.instances.all())
all_sgs = set([sg.group_name for sg in sgs])
all_inst_sgs = set([sg['GroupName'] for inst in insts for sg in inst.security_groups])
unused_sgs = all_sgs - all_inst_sgs
print 'Total SGs:', len(all_sgs)
print 'SGS attached to instances:', len(all_inst_sgs)
print 'Orphaned SGs:', len(unused_sgs)
print 'Unattached SG names:', unused_sgs

运行脚本时,我会收到以下错误

./list-unused-sq.py: line 1: import: command not found
./list-unused-sq.py: line 3: syntax error near unexpected token `('
./list-unused-sq.py: line 3: `ec2 = boto3.resource('ec2') #You have to change this line based on how you pass AWS credentials and AWS config'

有人可以指出我出了什么问题的地方,我需要做什么才能纠正它?

谢谢尼克

查看您的第一个错误行:

./list-unused-sq.py: line 1: import: command not found    

似乎您的问题与boto3无关,但在您的脚本中没有识别您的本地python。有关您的问题以及如何解决问题的更多信息

最新更新