2017-06-23 45 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