如何使用python将pcap转换为CSV

3qpi33ja  于 2022-12-06  发布在  Python
关注(0)|答案(4)|浏览(247)

我需要转换一个pcap文件,我有我的驱动器到一个csv文件使用python代码(我知道如何做它使用wireshark用户界面),但我需要通过python代码,
我已经在朋友圈里找到了这段代码:

import os
os.system("tshark -r mirai.pcap -T fields -e ip.src -e frame.len -e     ip.proto -E separatorr=, -E occurrence=f > traffic.csv")

我得到一个结果文件,但它是空的。
有人能帮我吗?

6rvt4ljy

6rvt4ljy1#

当我将命令改为:

os.system("tshark -r mirai.pcap -T fields -e ip.src -e frame.len -e     ip.proto -E separator=, -E occurrence=f > traffic.csv")

separatorr更改为separator
通常我使用包pysharkhttps://pypi.org/project/pyshark/)在python中处理我的pcap文件。

tzxcd3kk

tzxcd3kk2#

这是最简单的方法(在我看来)
os.system ('tshark -r'+in_file +'>'+ out_file +'.txt')
其中

in_file = <name of your pcap file>
out_file = <name of your output file>

PS:仅在python 3上测试

xfyts7mz

xfyts7mz3#

我使用subprocess以下面的方式执行此操作:

import subprocess

with open('/path/to/csv_file.csv','w') as f:
    subprocess.run("tshark -r /path/to/pcap_file.pcap -T fields
    -e frame.number -e ip.src -e ip.dst 
    -E header=y -E separator=/t".split(), stdout =f)

stdout写入'/path/to/csv_file.csv'

nbysray5

nbysray54#

您可以使用tshark和Python来自动执行此操作。

import os

for file in os.listdir('/path/to/pcap/files/'):
    output_csv = file + '.csv'
    os.system(f"tshark -N n -r ./test/{file} -T fields -e frame.number -e _ws.col.Time -e _ws.col.Source -e _ws.col.Destination -e _ws.col.Protocol -e _ws.col.Length -e _ws.col.Info -E header=y -E separator=, > {output_csv}")

为什么你没有得到一个空的csv的原因是你还没有安装tshark可用于您的CLI。在Linux中。尝试apt-install tshark,在Windows中,你必须安装Wireshark,然后设置环境变量到安装文件夹,使tshark激活到您的命令提示符。

相关问题