2017-06-23 10 views
1

私はBoto3スクリプトについてさらに理解しようとしています。Boto3未使用のセキュリティグループを見つける

私は仕事にここにPythonスクリプトを取得しようとしています同じ領域内のすべての

あるいくつかのVPCの内の未使用のセキュリティグループを検索したい: 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

ルック:あなたの問題は、あなたの地元のpythonを認識しないboto3でなく、スクリプトに関連していないよう

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

は思えます。 More info about your problem and how to solve it

+0

ありがとうございます。私は本当にあなたの返信に感謝し、それはまさに私の問題を解決しました! 。 。 '総SG数:78 インスタンスにSGSが接続されている:012 孤立SG:48 未接続SG名:set(['launch-wiz' – Nick

関連する問題