我正在尝试使用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);
打印的全部是“捕获'标准输入'”,但是与在命令行上运行命令相反,没有输出任何内容,并且从不打印处理过的数据包的数量。
在相同的tshark调用中尝试使用system()类似的行为也有奇怪的行为。
如果在程序仍在运行时尝试关闭新寡妇,将显示以下信息:(tshark.exe:8628):CaptureChild-WARNING **:sync_pipe_stop:强制儿童退出
您使用了错误的标志。 -i -
在标准输入上捕获,其中-i
在接口上捕获。要读取文件,请像这样使用-r <file>
:
const char * args = " -r <src file> > <dest file>";
我强烈建议签出tshark's manpage。
在此示例中,我们创建3个数据包捕获,使用tshark创建摘要文件,然后打印结果。这应该与tshark
和>
(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...