wireshark命令-python
wireshark.pcap文件很少。我需要将每个.pcap与传入和传出流量分开(通过提供源和目标mac地址),这些分开的文件必须写入两个不同的文件夹,即传入和传出。输出文件(作为传入和传出分开的文件)必须与输入文件同名,并且需要写入.csv文件。我尝试了下面的代码,但不起作用。非常感谢您的帮助。谢谢wireshark命令-python,python,wireshark,Python,Wireshark,wireshark.pcap文件很少。我需要将每个.pcap与传入和传出流量分开(通过提供源和目标mac地址),这些分开的文件必须写入两个不同的文件夹,即传入和传出。输出文件(作为传入和传出分开的文件)必须与输入文件同名,并且需要写入.csv文件。我尝试了下面的代码,但不起作用。非常感谢您的帮助。谢谢 import os import csv startdir= '/root/Desktop/Test' suffix= '.pcap' for root,dirs, files, in os.wa
import os
import csv
startdir= '/root/Desktop/Test'
suffix= '.pcap'
for root,dirs, files, in os.walk(startdir):
for name in files:
if name.endswith(suffix):
filename=os.path.join(root,name)
cmdOut = 'tshark -r "{}" -Y "wlan.sa==00:00:00:00:00:00 && wlan.da==11:11:11:11:11:11" -T fields -e frame.time_delta_displayed -e frame.len -E separator=, -E header=y > "{}"'.format(filename,filename)
cmdIn = 'tshark -r "{}" -Y "wlan.sa==11:11:11:11:11:11 && wlan.da==00:00:00:00:00:00" -T fields -e frame.time_delta_displayed -e frame.len -E separator=, -E header=y > "{}"'.format(filename,filename)
#os.system(cmd1)
#os.system(cmd2)
with open('/root/Desktop/Incoming/', 'w') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(os.system(cmdIn))
with open('/root/Desktop/Outgoing/', 'w') as csvFile:
writer = csv.writer(csvFile)
writer.writerows(os.system(cmdOut))
csvFile.close()
正确的实现可能更像:
import csv
import os
import subprocess
startdir = 'in.d' # obviously, people other than you won't have /root/Desktop/test
outdir = 'out.d'
suffix = '.pcap'
def decode_to_file(cmd, in_file, new_suffix):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
fileName = outdir + '/' + in_file[len(startdir):-len(suffix)] + new_suffix
os.makedirs(os.path.dirname(fileName), exist_ok=True)
csv_writer = csv.writer(open(fileName, 'w'))
for line_bytes in proc.stdout:
line_str = line_bytes.decode('utf-8')
csv_writer.writerow(line_str.strip().split(','))
for root, dirs, files in os.walk(startdir):
for name in files:
if not name.endswith(suffix):
continue
in_file = os.path.join(root, name)
cmdCommon = [
'tshark', '-r', in_file,
'-T', 'fields',
'-e', 'frame.time_delta_displayed',
'-e', 'frame.len',
'-E', 'separator=,',
'-E', 'header=y',
]
decode_to_file(
cmd=cmdCommon + ['-Y', 'wlan.sa==00:00:00:00:00:00 && wlan.da==11:11:11:11:11:11'],
in_file=in_file,
new_suffix='.out.csv'
)
decode_to_file(
cmd=cmdCommon + ['-Y', 'wlan.sa==11:11:11:11:11:11 && wlan.da==00:00:00:00:00:00'],
in_file=in_file,
new_suffix='.in.csv'
)
注:
- 我们不使用
。(这是行不通的,因为它返回的是数字退出状态,而不是可以写入CSV文件格式的字符串)os.system()
- 我们不需要生成任何临时文件;我们可以直接从
子流程的stdout读入Python代码tshark
- 我们通过修改输入文件名来构造输出文件名(将其扩展名分别替换为
和.out.csv
).in.csv
- 因为
需要一个iterable,所以我们可以通过逐行拆分来生成一个iterablewriterow()
请注意,我不完全清楚您为什么要使用Python CSV模块,因为
字段
输出似乎已经是CSV,因此您也可以直接将输出重定向到文件,而无需其他处理。正确的实现可能更像:
import csv
import os
import subprocess
startdir = 'in.d' # obviously, people other than you won't have /root/Desktop/test
outdir = 'out.d'
suffix = '.pcap'
def decode_to_file(cmd, in_file, new_suffix):
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
fileName = outdir + '/' + in_file[len(startdir):-len(suffix)] + new_suffix
os.makedirs(os.path.dirname(fileName), exist_ok=True)
csv_writer = csv.writer(open(fileName, 'w'))
for line_bytes in proc.stdout:
line_str = line_bytes.decode('utf-8')
csv_writer.writerow(line_str.strip().split(','))
for root, dirs, files in os.walk(startdir):
for name in files:
if not name.endswith(suffix):
continue
in_file = os.path.join(root, name)
cmdCommon = [
'tshark', '-r', in_file,
'-T', 'fields',
'-e', 'frame.time_delta_displayed',
'-e', 'frame.len',
'-E', 'separator=,',
'-E', 'header=y',
]
decode_to_file(
cmd=cmdCommon + ['-Y', 'wlan.sa==00:00:00:00:00:00 && wlan.da==11:11:11:11:11:11'],
in_file=in_file,
new_suffix='.out.csv'
)
decode_to_file(
cmd=cmdCommon + ['-Y', 'wlan.sa==11:11:11:11:11:11 && wlan.da==00:00:00:00:00:00'],
in_file=in_file,
new_suffix='.in.csv'
)
注:
- 我们不使用
。(这是行不通的,因为它返回的是数字退出状态,而不是可以写入CSV文件格式的字符串)os.system()
- 我们不需要生成任何临时文件;我们可以直接从
子流程的stdout读入Python代码tshark
- 我们通过修改输入文件名来构造输出文件名(将其扩展名分别替换为
和.out.csv
).in.csv
- 因为
需要一个iterable,所以我们可以通过逐行拆分来生成一个iterablewriterow()
注意,我不完全清楚为什么要使用Python CSV模块,因为
字段
输出似乎已经是CSV,因此,您也可以直接将输出重定向到一个文件,而无需其他处理。请将您的代码添加为文本而不是图像您没有为传入和传出的csv文件使用正确的文件名。还有,为什么需要csvFile.close()
?@kuro抱歉,我是python新手,我不知道哪里出错了。我应该为文件名写什么?Thanks@kuro我将代码添加为文本。使用字符串连接来形成命令是一个严重的安全问题(这意味着很难安全地使用os.system()
)。如果切换到subprocess.run()
而不选择shell=True
,则执行正确的操作要容易得多,因为您可以将重定向以外的所有内容作为列表元素传递。请将代码添加为文本而不是图像。您没有为传入和传出的csv文件使用正确的文件名。还有,为什么需要csvFile.close()
?@kuro抱歉,我是python新手,我不知道哪里出错了。我应该为文件名写什么?Thanks@kuro我将代码添加为文本。使用字符串连接来形成命令是一个严重的安全问题(这意味着很难安全地使用os.system()
)。如果您切换到subprocess.run()
而不使用shell=True
,那么做正确的事情就容易多了,因为您可以将重定向以外的所有内容作为列表元素传递。非常感谢。我运行代码。但我的错误率越来越低。我用谷歌搜索来修复它,但找不到解决方案。使用csv.writer(open(out FileName,'w'))作为csv:attribute error enter,因此您使用的Python版本没有csv对象作为上下文管理器。只需将其存储为正则变量,并显式关闭它即可。我已经编辑以演示这种变化。我正在使用Pycharm。项目解释器是Python 3.7。我像你一样更改了代码。但现在我发现了这些错误。tshark:输出字段用“-e”指定,但“-Tek、-Tfields、-Tjson或-Tpdml未指定。为了检查我在linux终端上运行了相同的tshark命令,它给出了相同的错误。另外,当我运行这个python代码时,它会给出另一个错误。AttributeError:“_csv.writer”对象没有属性“close”,我在linux终端中用-T字段而不是-T选项卡键入了tshark命令,然后它就可以正常工作了。如果我们在python代码中做了这样的更改,这会不会是一个问题,因为您已经说过,使用生成器进一步拆分选项卡上的每一行很容易?tabs
在wireshark 3.0.1中有效;您正在运行哪个版本?非常感谢。我运行代码。但我的错误率越来越低。我用谷歌搜索来修复它,但找不到解决方案。使用csv.writer(open(out FileName,'w'))作为csv:attribute error enter,因此您使用的Python版本没有csv对象作为上下文管理器。只需将其存储为正则变量,并显式关闭它即可。我已经编辑以演示这种变化。我正在使用Pycharm。项目解释器是Python 3.7。我像你一样更改了代码。但现在我发现了这些错误。tshark:输出字段用“-e”指定,但“-Tek、-Tfields、-Tjson或-Tpdml未指定。为了检查我在linux终端上运行了相同的tshark命令,它给出了相同的错误。另外,当我运行这个python代码时,它会给出另一个错误。归属者