【发布时间】:2021-09-30 20:13:00
【问题描述】:
【问题讨论】:
【问题讨论】:
我想分享我在深夜想出的解决方案,将来自其他解决方案的部分拼凑起来 - 主要来自 @Abdul Gill 和 hist ec2_sg_rules 脚本。
import boto3
# Explicitly declaring variables here grants them global scope
cidr_block = ""
ip_protpcol = ""
from_port = ""
to_port = ""
from_source = ""
description = ""
sg_filter = [{'Name': 'group-name', 'Values': ['*ssh*']}]
print("%s,%s,%s" % ("Group-Name","Group-ID","CidrIp"))
ec2 = boto3.client('ec2' )
sgs = ec2.describe_security_groups(Filters=sg_filter)["SecurityGroups"]
for sg in sgs:
group_name = sg['GroupName']
group_id = sg['GroupId']
# print("%s,%s" % (group_name,group_id))
# InBound permissions ##########################################
inbound = sg['IpPermissions']
for rule in inbound:
#Is source/target an IP v4?
if len(rule['IpRanges']) > 0:
for ip_range in rule['IpRanges']:
cidr_block = ip_range['CidrIp']
if 'Description' not in ip_range:
if '10.' not in cidr_block:
print("%s,%s,%s" % (group_name, group_id, cidr_block))
print('\n')
希望这对其他人有所帮助。
【讨论】: