gpt4 book ai didi

在 C 中使用 popen 捕获 tshark 标准输出

转载 作者:行者123 更新时间:2023-11-30 17:28:00 27 4
gpt4 key购买 nike

我正在尝试通过 C 语言程序捕获 tshark 的标准输出。为此,我使用 popen() 调用来打开 tshark 进程并从返回的 FILE 流中读取。

代码示例:

#include <stdio.h>
#include <stdlib.h>

int main() {

FILE* pipe_fd = popen("tshark -i eth0 -R icmp -2 -T fields -e icmp.checksum -e icmp.seq", "r");
//FILE* pipe_fd = popen("lsof", "r");

if (!pipe_fd) {
fprintf(stderr, "popen failed.\n");
return EXIT_FAILURE;
}

char buffer[2048];
while (NULL != fgets(buffer, sizeof(buffer), pipe_fd)) {
fprintf(stdout, "SO: %s", buffer);
}

pclose(pipe_fd);
printf("tdr FINISHED!\n");
return 0;
}
<小时/>

当我运行它时,我只得到数据包编号计数,即,我没有得到数据包字段信息,只是得到数据包计数,每个数字都覆盖同一位置的前一个数字(没有行滚动发生)。

类似这样的东西,我猜是 4 个数据包:

tomas@ubuntu64:~$ sudo ./main
tshark: Lua: Error during loading:
[string "/usr/share/wireshark/init.lua"]:46: dofile has been disabled due to running Wireshark as superuser. See http://wiki.wireshark.org/CaptureSetup/CapturePrivileges for help in running Wireshark as an unprivileged user.
Running as user "root" and group "root". This could be dangerous.
Capturing on 'eth0'
4

但是,如果我在 C 程序中将“tshark”命令参数更改为“lsof”,我就能得到标准输出。

tomas@ubuntu64:~$ sudo ./main
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.
SO: COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
SO: init 1 root cwd DIR 8,1 4096 2 /
SO: init 1 root rtd DIR 8,1 4096 2 /
SO: init 1 root txt REG 8,1 265848 791529 /sbin/init
SO: init 1 root mem REG 8,1 47712 824514 /lib/x86_64-linux-gnu/libnss_files-2.19.so
...

根据这个结果,我假设 tshark 将信息发送到标准输出的方式有一些特殊之处。有人知道这种行为吗?我要检查 tshark 源代码,看看它是否可以澄清它。

最后一点。

当我通过 shell 运行 tshark 时,经常会丢失数据包编号,例如:

tomas@ubuntu64:~$ sudo tshark -i eth0 -R icmp -2 -T fields -e icmp.checksum -e icmp.seq
tshark: Lua: Error during loading:
[string "/usr/share/wireshark/init.lua"]:46: dofile has been disabled due to running Wireshark as superuser. See http://wiki.wireshark.org/CaptureSetup/CapturePrivileges for help in running Wireshark as an unprivileged user.
Running as user "root" and group "root". This could be dangerous.
Capturing on 'eth0'
0x0ee5 63045
1 0x8ae3 63046
2 0xcfdf 63047
3 0xe4d9 63048
4 0x9db7 63049
5 0x6798 63050
6 0x0175 63051
7 0x9e54 63052
0xa654 63052
9 0xe050 63053
0xe850 63053
11 0x8389 63054
0x8b89 63054
13 0x9b81 63055
0xa381 63055

缺少打印的数据包编号 8、10、12、14。

当我将标准输出重定向到文件时,它不会发送计数:

tomas@ubuntu64:~$ sudo tshark -i eth0 -R icmp -2 -T fields -e icmp.checksum -e icmp.seq > kk
tomas@ubuntu64:~$ cat kk
0x2073 63321
0x2873 63321
0x7c6a 63322

tshark 以不同方式处理打印数据包计数和信息的另一个线索。

问候,汤姆

最佳答案

好吧,即使我最终不使用这种与 tshark 一起工作的方式,我想我找到了用于popen tshark 的选项。在手册页中,选项 -l:

Flush the standard output after the information for each packet is printed. (This is not, strictly speaking, line-buffered if -V was specified; however, it is the same as line-buffered if -V wasn't specified, as only one line is printed for each packet, and, as -l is normally used when piping a live capture to a program or script, so that output for a packet shows up as soon as the packet is seen and dissected, it should work just as well as true line-buffering. We do this as a workaround for a deficiency in the Microsoft Visual C++ C library.)

This may be useful when piping the output of TShark to another program, as it means that the program to which the output is piped will see the dissected data for a packet as soon as TShark sees the packet and generates that output, rather than seeing it only when the standard output buffer containing that data fills up.

我测试了一下,似乎有效。以防万一有人需要它。

关于在 C 中使用 popen 捕获 tshark 标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26177333/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com