Skip to content

Instantly share code, notes, and snippets.

@marceloneppel
Forked from deusebio/tcpdump_parser.py
Created September 20, 2023 12:00
Show Gist options
  • Save marceloneppel/b54c6247e78ce0ab0421b5d1684e94e9 to your computer and use it in GitHub Desktop.
Save marceloneppel/b54c6247e78ce0ab0421b5d1684e94e9 to your computer and use it in GitHub Desktop.
Parse tcpdump output
import pandas as pd
import re
space_splitter = re.compile("\s+")
regex = re.compile("\s*(.*)\s*>\s*(.*?):\s.*")
def parse_line(line):
try:
elements = space_splitter.split(line)
source_dest = regex.match(" ".join(elements[4:])).groups()
except Exception:
return None
return tuple(elements[:4]) + tuple(x.strip() for x in source_dest)
with open("./output.txt", "r") as fid:
lines = fid.readlines()
items = [parsed + (idx,) for idx, line in enumerate(lines) if (parsed := parse_line(line))]
non_records = [idx for idx, line in enumerate(lines) if not parse_line(line)]
df = pd.DataFrame.from_records(items, columns=["time", "network", "direction", "class", "source", "target", "idx"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment