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'
就是有人能指出哪里我已经错的,什么我需要做纠正它吗?
感谢 尼克
非常感谢你。我非常感谢你的回复,这正是解决我的问题的方法! 。 。 '总的SG:78个 SGS连接到实例:30层 孤立的SG:48 未附加SG名:组(['发射-wiz' – Nick