python - Boto3查找未使用的安全组

标签 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'

有人能够指出我哪里出了问题以及我需要做什么来纠正它吗?

谢谢 尼克

最佳答案

看看你的第一个错误行:

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

看来您的问题与 boto3 无关,但在您的脚本中无法识别您的本地 python。 More info about your problem and how to solve it

关于python - Boto3查找未使用的安全组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44719530/

相关文章:

python - 合并任意数量的列表字典

python - 访问 lmfit 中的 params 属性

amazon-s3 - 如何监控 Amazon S3 服务中对象的带宽?

javascript - 爬取 javascript 表但考虑异常请求

python - 在 MySQL 中插入 datetime.datetime 对象

python - Funcparserlib.lexer.Spec 导入错误 : cannot import name 'Spec'

google-app-engine - 处理 appengine 中的子域

python - 使用 exchangelib 将电子邮件标记为已读

amazon-web-services - S3 PutObject 上的 Lambda 访问被拒绝

c# - 如何添加对 AWS SQS 队列的权限?