AWS Infra

[ AWS ] boto3를 이용한 보안그룹 소스IP 체크

Pydole 2022. 7. 11. 10:32

 

import boto3
from botocore.exceptions import ClientError

ec2 = boto3.client('ec2')

ips = 'x.x.x.x/32'

def sg_check():
    response = ec2.describe_security_groups()['SecurityGroups']

    for i in response:
        sgid = i['GroupId']

        for j in i['IpPermissions']:
            for x in j['IpRanges']:
                if x['CidrIp'] == ips:
                    print(sgid,x['CidrIp'],x['Description'])


if __name__ == "__main__":
    sg_check()