我正在尝试了解有关 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'
有人能够指出我哪里出了问题以及我需要做什么来纠正它吗?
谢谢 尼克
最佳答案
看看你的第一个错误行:
./list-unused-sq.py: line 1: import: command not found
看来您的问题与 boto3 无关,但在您的脚本中无法识别您的本地 python。 More info about your problem and how to solve it
关于python - Boto3查找未使用的安全组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44719530/