【问题标题】:Boto3 Finding unused Security GroupsBoto3 查找未使用的安全组
【发布时间】:2017-11-26 22:06:51
【问题描述】:

我正在尝试更多地了解 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'

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

谢谢 尼克

【问题讨论】:

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


    【解决方案1】:

    查看您的第一个错误行:

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

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

    【讨论】:

    • 非常感谢。非常感谢您的回复,这正是解决我的问题的原因! . . Total SGs: 78 SGS attached to instances: 30 Orphaned SGs: 48 Unattached SG names: set(['launch-wiz
    猜你喜欢
    • 2017-04-29
    • 2015-09-02
    • 2014-09-01
    • 2019-01-18
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 2016-04-11
    • 2017-03-08
    相关资源
    最近更新 更多