Skip to content

Instantly share code, notes, and snippets.

@sulrich
Created July 10, 2024 19:18
Show Gist options
  • Save sulrich/20b33bcb688d2858b5a241dd2d13edf2 to your computer and use it in GitHub Desktop.
Save sulrich/20b33bcb688d2858b5a241dd2d13edf2 to your computer and use it in GitHub Desktop.
script to parse GoBMP dump outputs
#!/usr/bin/env python3
import json
import base64
import argparse
import pprint as pprint
import sys
def parse_logentry(entry):
parsed_entry = {}
for k, v in entry.items():
val = ""
if k == "key":
# keys are just the router_hash base64 encoded
val = base64.b64decode(v).decode("utf-8")
elif k == "value":
# values are json dicts that have been base64 encoded and need to
# be unfurled into json objects.
val = json.loads(base64.b64decode(v).decode("utf-8"))
else:
val = v
parsed_entry[k] = val
return parsed_entry
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
dest="bmplog",
help="GoBMP log file to parse",
)
args = parser.parse_args()
try:
with open(args.bmplog) as f:
for i in f.readlines():
d = json.loads(i)
logentry = parse_logentry(d)
pprint.pprint(logentry)
except IOError:
print("error opening log file:", args.bmplog)
sys.exit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment