I'm having trouble with my program that writes parts of a pcap file to a new json file. The issue is that the last 100 lines of the file are only visible when I manually stop the program in the terminal. It only writes the last lines if I forcefully kill it.
file_name = "my_file.pcap"
output_file = 'output_json.json'
start_time = datetime.datetime(2023, 3, 24, 11, 57, 39)
end_time = datetime.datetime(2023, 3, 24, 11, 57, 40)
command = [
"tshark", "-r", file_name, "-Y",
f'(oran_fh_cus.extType == 11) && (frame.time >= "{start_time.strftime("%Y-%m-%d %H:%M:%S")}" && frame.time <= "{end_time.strftime("%Y-%m-%d %H:%M:%S")}")',
"-T", "json", "-e", "frame.time", "-e", "frame.number", "-e", "oran_fh_cus.extType",
"-e", "oran_fh_cus.bfwI", "-e", "oran_fh_cus.bfwQ"
]
with open(output_file, "w") as f:
subprocess.run(command, stdout=f)
I encounter the following errors before stopping the program:
"Unexpected end of string" "Expected comma or closing bracket"
However, when I manually stop the program, the last lines are added successfully, and the line that triggered the error appears to be completely normal.
This is happening for two reasons:
-lflag (see man page docs below). That will make it print each packet as they are received.]. If that doesn't work for you, I think you will have to switch to some other output format.