【问题标题】:Using boto3 to retrieve a SG rule Description使用 boto3 检索 SG 规则说明
【发布时间】:2021-09-30 20:13:00
【问题描述】:

我们正在尝试使用 boto3 来查询我们的 AWS 实例并获取所有具有没有描述的规则的安全组。我无法弄清楚如何检索规则描述。我在下面包含了我要检索的列的图像。

【问题讨论】:

    标签: python boto


    【解决方案1】:

    我想分享我在深夜想出的解决方案,将来自其他解决方案的部分拼凑起来 - 主要来自 @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')
    

    希望这对其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 1970-01-01
      • 2015-09-22
      相关资源
      最近更新 更多