wireshark 命令 - python
wireshark commands - python
enter image description herewireshark .pcap 文件很少。我需要将每个 .pcap 与传入和传出流量分开(通过提供源和目标 mac 地址),这些分开的文件必须写入两个不同的文件夹,即传入和传出。输出文件(传入和传出分离的文件)必须与输入文件同名,并且需要写入 .csv 文件。我试过下面的代码,但没有用。任何帮助是极大的赞赏。谢谢
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'
)
注:
- 我们不使用
os.system()
。 (这是行不通的,因为它 returns 一个数字退出状态,而不是可以写入 CSV 文件的格式的字符串)。
- 我们不需要生成任何临时文件;我们可以直接从
tshark
子进程的标准输出中读取我们的 Python 代码。
- 我们通过修改输入文件名(分别用
.out.csv
和 .in.csv
替换其扩展名)来构造我们的输出文件名。
- 因为
writerow()
需要一个iterable,我们可以通过逐行拆分生成一个。
请注意,我不完全清楚您为什么要使用 Python CSV 模块,因为 fields
输出似乎已经是 CSV,因此也可以重定向直接输出到一个文件,没有其他处理。
enter image description herewireshark .pcap 文件很少。我需要将每个 .pcap 与传入和传出流量分开(通过提供源和目标 mac 地址),这些分开的文件必须写入两个不同的文件夹,即传入和传出。输出文件(传入和传出分离的文件)必须与输入文件同名,并且需要写入 .csv 文件。我试过下面的代码,但没有用。任何帮助是极大的赞赏。谢谢
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'
)
注:
- 我们不使用
os.system()
。 (这是行不通的,因为它 returns 一个数字退出状态,而不是可以写入 CSV 文件的格式的字符串)。 - 我们不需要生成任何临时文件;我们可以直接从
tshark
子进程的标准输出中读取我们的 Python 代码。 - 我们通过修改输入文件名(分别用
.out.csv
和.in.csv
替换其扩展名)来构造我们的输出文件名。 - 因为
writerow()
需要一个iterable,我们可以通过逐行拆分生成一个。
请注意,我不完全清楚您为什么要使用 Python CSV 模块,因为 fields
输出似乎已经是 CSV,因此也可以重定向直接输出到一个文件,没有其他处理。