【问题标题】:How to call tshark to convert a capture file to txt with CreateProcess如何使用 CreateProcess 调用 tshark 将捕获文件转换为 txt
【发布时间】:2019-11-25 13:26:50
【问题描述】:

我正在尝试从用 Visual Studio 编译的 c++ 程序调用 tshark。有些电话有效,但有些电话无效。我能够启动捕获到文件:

STARTUPINFO startupInfo={sizeof(startupInfo)};
PROCESS_INFORMATION processInfo;
const char * args = " -i \"Ethernet 9\" -w C:\\Users\\raymond\\Documents\\Debug\\Ethernet_9.cap -b duration:90 -b files:2\"";

CreateProcess("C:\\Program Files\\Wireshark\\tshark.exe", const_cast<char *>(ss.str().c_str()), NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &processInfo);

但我无法将捕获文件转换为文本:

STARTUPINFO startupInfo={sizeof(startupInfo)};
PROCESS_INFORMATION processInfo;
const char * args = " -i - < \"C:\\Users\\raymond\\Documents\\Ethernet_9.cap\" > \"C:\\Users\\raymond\\Documents\\Ethernet_9.txt";

CreateProcess("C:\\Program Files\\Wireshark\\tshark.exe", const_cast<char *>(ss.str().c_str()), NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &processInfo);

所有打印的都是“Capturing on 'Standard input'”,但与在命令行上运行命令相反,没有输出任何内容,并且从不打印处理的数据包数。

用 system() 尝试类似的东西在同一个 tshark 调用上也会有奇怪的行为。

如果我在程序仍在运行时尝试关闭新寡妇,则会打印以下内容: (tshark.exe:8628): CaptureChild-WARNING **: sync_pipe_stop: 强制孩子退出

【问题讨论】:

  • 你可以使用boost process 还是你需要原始的windows API 没有任何顶部的包装?您实际上需要管道“子进程”输出,打开文件流并将结果写入文件。

标签: c++ windows visual-studio wireshark tshark


【解决方案1】:

您使用了错误的标志。 -i - 捕获标准输入,-i 捕获接口。要从文件中读取,请使用-r &lt;file&gt;,如下所示:

const char * args = " -r <src file> > <dest file>";

我强烈建议您查看tshark's manpage

完整示例

在这个例子中,我们创建了一个 3 抓包,使用 tshark 创建一个摘要文件,并打印结果。这应该像 tshark&gt; 一样跨平台(Windows、Linux、Macos、BSD 等)。

// example.cpp
#include <iostream>
#include <fstream>

int main() {
    system("tshark -w source.pcap -c 3");
    system("tshark -r source.pcap > dest.txt");

    std::ifstream dest;
    dest.open("dest.txt");
    std::cout << dest.rdbuf();
    dest.close();

    return 0;
}

编译后,我们可以看到tshark的反馈和前几个包。

rj$ ./example.o
Capturing on 'Wi-Fi: en0'
3
    1   0.000000 172.31.99.198 → resolver1.opendns.com DNS  80 Standard...     
    2   0.000026 172.31.99.198 → 217.221.186.35.bc.googleusercontent.co...
    3   0.000026 172.31.99.198 → ec2-3-231-46-251.compute-1.amazonaws.c...

【讨论】:

  • -r 似乎和 -i 一样在命令行上工作。它根本不适用于 CreateProcess()。我认为问题在于 Victor_Gubin 所说的输出管道。
  • @rur2641 添加了完整示例。有很多方法可以让它工作 - 这是最简单的方法之一。
【解决方案2】:

您不能对 CreateProcess 使用重定向操作 - 重定向由命令处理器 cmd.exe 处理,当您使用 CreateProcess 时会绕过它。

您可能只想使用 C 中的 system 命令,该命令使用系统 shell(Windows 上为 cmd.exe):

system("C:\\Program Files\\Wireshark\\tshark.exe -r \"C:\\Users\\raymond\\Documents\\Ethernet_9.cap\" > \"C:\\Users\\raymond\\Documents\\Ethernet_9.txt\"");

正如@RossJacobs 还提到的,您需要使用-r 而不是-i 来读取文件。

【讨论】:

    猜你喜欢
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    相关资源
    最近更新 更多