Skip to content

Instantly share code, notes, and snippets.

@SmugZombie
Last active November 16, 2020 21:17
Show Gist options
  • Select an option

  • Save SmugZombie/27915068c212fb11611e41393aa4698d to your computer and use it in GitHub Desktop.

Select an option

Save SmugZombie/27915068c212fb11611e41393aa4698d to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import json
# Ron Egli
# v0.4.1
# Quick and dirty script used to Pull all rules that have a specific IP defined to compare against the rules of another specific IP
# aws ec2 describe-security-groups > SecGroups.json
# View in table: python3 dirtysecgroup.py | column -t -s, | less -S
with open('SecGroups.json', 'r') as reader:
SecGroups = reader.read()
SEARCH_IPS = ["10."]
SecGroups = json.loads(SecGroups)['SecurityGroups']
print("Security Group, From Port, To Port, IP, Description")
for SecurityGroup in SecGroups:
dump = json.dumps(SecurityGroup)
for SEARCH_IP in SEARCH_IPS:
if SEARCH_IP in dump:
print(SecurityGroup['GroupName']+" ("+SecurityGroup['GroupId']+"),,,,")
for Rule in SecurityGroup['IpPermissions']:
ruledump = json.dumps(Rule)
if SEARCH_IP in ruledump:
Description = ""
CidrIP = SEARCH_IP
FromPort = "N/A"
ToPort = "N/A"
if str(Rule['IpProtocol']) == "-1":
FromPort = "Any"
ToPort = "Any"
IPS = Rule['IpRanges']
for IP in IPS:
if SEARCH_IP in IP['CidrIp']:
CidrIP = IP['CidrIp']
try:
Description = IP['Description']
if Description == "":
Description = "N/A"
except:
Description = "N/A"
try:
FromPort = str(Rule['FromPort'])
except:
FromPort = FromPort
try:
ToPort = str(Rule['ToPort'])
except:
ToPort = ToPort
print(" ,"+FromPort+","+ToPort+","+CidrIP+","+Description)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment