Created
September 16, 2022 11:56
-
-
Save developerck/664031cb59889704145cfb3f92e6985d to your computer and use it in GitHub Desktop.
check security groups in a region if linked to a instance or not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python3 | |
import re | |
from collections import OrderedDict | |
from pprint import pprint | |
import boto3 | |
ec2 = None | |
# define your region and run in cloud shell | |
# python3 ./sg-check.py | |
ec2 = boto3.client('ec2', 'us-east-1') | |
linked = [] | |
notlinked =[] | |
for sg in ec2.describe_security_groups()['SecurityGroups']: | |
nw = ec2.describe_network_interfaces( Filters=[{'Name': 'group-id','Values': [sg['GroupId']]}]); | |
nw = nw['NetworkInterfaces'] | |
if not nw : | |
str = sg['GroupName']+' | '+sg['GroupId']+ ': Not Linked ' | |
notlinked.append(str); | |
else: | |
str = sg['GroupName']+' | '+sg['GroupId']+ ': Linked ' | |
linked.append(str) | |
print("===Linked==="); | |
print("\n".join(linked)); | |
print("===Not Linked==="); | |
print("\n".join(notlinked)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment