2017-06-23 19 views
1

我想了解更多关于Boto3脚本。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

看看你的第一个错误行:

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

看起来像你的问题是不是与boto3但在你的脚本不能识别本地蟒蛇有关。 More info about your problem and how to solve it

+0

非常感谢你。我非常感谢你的回复,这正是解决我的问题的方法! 。 。 '总的SG:78个 SGS连接到实例:30层 孤立的SG:48 未附加SG名:组(['发射-wiz' – Nick