cleanup: pcapConverter added comments and cleaned-up

This commit is contained in:
uhi22 2023-04-18 08:04:09 +02:00
parent 4a8dbe2d4e
commit 6e70eee176

View file

@ -1,7 +1,7 @@
# pcap Converter # pcap Converter
# #
# This little helper tool read a network trace (e.g. recorded with wireshark) and # This little helper tool reads a network trace (e.g. recorded with wireshark) and
# interprets the content of the EXI.V2GTP.TCP.IPv6 data. # interprets the content of the EXI.V2GTP.TCP.IPv6 data.
# #
# Preconditions: # Preconditions:
@ -9,15 +9,34 @@
# 2. You installed the python library pyshark, according to https://github.com/KimiNewt/pyshark/ # 2. You installed the python library pyshark, according to https://github.com/KimiNewt/pyshark/
# pip install pyshark # pip install pyshark
# 3. You cloned and compiled the OpenV2Gx EXI decoder from https://github.com/uhi22/OpenV2Gx # 3. You cloned and compiled the OpenV2Gx EXI decoder from https://github.com/uhi22/OpenV2Gx
#
# Limitations:
# - Only DIN is supported at the moment.
# - The script treats all V2G EXI messages as DIN messages. This means, the ApplHandshake messages at
# the begin of the charging session will lead to wrong or not decoded data.
# - The path where the script look for pcap files needs to be configured in the code.
#
# Possible improvements / Todos:
# - Show also the SLAC, NeigborDiscovery and SDP.
# - Add flexibility to also decode the ApplHandshake messages.
# - Add ISO support.
# - Configure the path where to look for pcap files via command line
#
import pyshark import pyshark
import exiConnector import exiConnector
import os import os
# The path where the script will search for pcap files:
directory = '../temp'
def convertPcapToTxt(inputFileName): def convertPcapToTxt(inputFileName):
cap = pyshark.FileCapture(inputFileName, display_filter="ipv6") cap = pyshark.FileCapture(inputFileName, display_filter="ipv6")
fileOut = open(inputFileName + '.decoded.txt', 'w') fileOut = open(inputFileName + '.decoded.txt', 'w')
print("# generated by pcapConverter.py", file=fileOut)
print("# https://github.com/uhi22/pyPLC", file=fileOut)
# Example how to access the data:
#print(cap) #print(cap)
#print(cap[0]) #print(cap[0])
#print(cap[1]) #print(cap[1])
@ -45,17 +64,14 @@ def convertPcapToTxt(inputFileName):
print(decoded, file=fileOut) print(decoded, file=fileOut)
fileOut.close() fileOut.close()
# assign directory
directory = '../temp'
# iterate over files in # iterate over files in the directory
# that directory
for filename in os.listdir(directory): for filename in os.listdir(directory):
f = os.path.join(directory, filename) strFileNameWithPath = os.path.join(directory, filename)
# checking if it is a file # checking if it is a file
if os.path.isfile(f): if os.path.isfile(strFileNameWithPath):
print(f) print(strFileNameWithPath)
if (f[-5:]==".pcap") or (f[-7:]==".pcapng"): # check the file extension:
strFileNameWithPath = f if (strFileNameWithPath[-5:]==".pcap") or (strFileNameWithPath[-7:]==".pcapng"):
print("Will decode " + strFileNameWithPath) print("Will decode " + strFileNameWithPath)
convertPcapToTxt(strFileNameWithPath) convertPcapToTxt(strFileNameWithPath)