Project

General

Profile

New Model #3509 » pcap2stream.py

converts pcap capture into lines for test.py - James Lieb, 04/19/2018 01:40 PM

 
#! /usr/bin/python

# process the output of a tshark stream to strip blank lines and re-join folded
# lines

import sys

lineout = ""
lastdir = "X"
lineno = 0

for line in sys.stdin:
lineno += 1
line = line.replace('\n', '') # chomp
if (len(line) == 0):
continue
toks = line.split("\t")
if (toks[0] == "0x00000002"): # some kind of control msg
toks[0] = "C"
elif (toks[0] == "0x00000003"): # this is bulk in/out data
toks[0] = "D"
else:
print("Bogus USB %s at line %d" % (toks[0], lineno))
continue
if (toks[1] == "host"): # We are the host so we do the writing...
toks[1] = "W"
else:
toks[1] = "R"

if (toks[0] == "C"):
if (len(lineout) > 0):
print("%s: %s" % (lastdir, lineout))
lineout = ""
lastdir = "X"
else:
if (toks[1] == lastdir):
if (len(toks) > 2):
lineout += (" " + toks[2].replace(":", ' '))
else:
if (len(lineout) > 0):
print("%s: %s" % (lastdir, lineout))
lastdir = toks[1]
lineout = toks[2].replace(":", ' ')
if (len(lineout) > 0):
print("%s: %s" % (lastdir, lineout))
(4-4/11)