Boto3查找未使用的安全组

时间:2017-06-23 10:45:49

标签: python python-2.7 amazon-web-services

我想了解更多有关Boto3脚本的信息。

我想在几个VPC中搜索全部位于同一区域的未使用的安全组

我想让python脚本在这里工作: boto3 searching unused security groups

所以我的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'

有人能够指出我出错的地方以及我需要做些什么来纠正它?

由于 尼克

1 个答案:

答案 0 :(得分:1)

查看您的第一个错误行:

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

好像你的问题与boto3无关,但在你的脚本中没有识别你的本地python。 More info about your problem and how to solve it