Created
October 5, 2016 01:24
-
-
Save jwoglom/c88a9e66af61cde7c09c400dfa99fa10 to your computer and use it in GitHub Desktop.
Graph EC2 security groups
This file contains hidden or 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
import json | |
import argparse | |
import subprocess | |
import tempfile | |
def main(): | |
parser = argparse.ArgumentParser(description='Visualize AWS Security Groups') | |
parser.add_argument('--file', dest='file', action='store', help='JSON output of `aws ec2 describe-security-groups`') | |
parser.add_argument('--output', dest='output', action='store', help='SVG output filename') | |
args = parser.parse_args() | |
out = args.output or "output.svg" | |
if args.file: | |
print("Importing JSON file...") | |
jsdata = open(args.file, "r").read() | |
c = conv(jsdata.decode()) | |
else: | |
print("Running `aws ec2 describe-security-groups`...") | |
sp = subprocess.Popen(["aws", "ec2", "describe-security-groups"], stdin=subprocess.PIPE, stdout=subprocess.PIPE) | |
output, err = sp.communicate() | |
out = output.decode() | |
if len(out) == 0 or out[0] != "{": | |
print("Error running `aws ec2 describe-security-groups`...") | |
print(out) | |
return | |
c = conv(out) | |
tosvg(c, out) | |
def conv(jsdata): | |
js = json.loads(jsdata) | |
d = js["SecurityGroups"] | |
for i in d: | |
name = None | |
for j in i["Tags"]: | |
if j["Key"] == "Name": | |
i["GroupName"] = j["Value"] | |
tf = tempfile.NamedTemporaryFile(delete=False) | |
tf.write(json.dumps(js).encode()) | |
return tf.name | |
def tosvg(fname, out): | |
print("Running `aws_security_viz`...") | |
subprocess.Popen(["aws_security_viz", "-o", fname, "-f", out]).communicate() | |
print("Saved as", out) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment