【发布时间】:2018-05-23 13:02:08
【问题描述】:
我想将tcpdump 的文本输出写入压缩文件。
首先我尝试了最明显的:
# tcpdump -l -i eth0 | gzip -c > test.gz
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
^C63 packets captured
244 packets received by filter
0 packets dropped by kernel
4 packets dropped by interface
# file test.gz
test.gz: empty
#
然后我为Debian 9 (Stretch)找到了以下解决方案:
# tcpdump -l -i eth0 | ( gzip -c > test.gz & )
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
^C150 packets captured
160 packets received by filter
0 packets dropped by kernel
# file test.gz
test.gz: gzip compressed data, last modified: Wed May 23 12:56:16 2018, from Unix
#
这在 Debian 9 (Stretch) 上运行良好,但在 Debian 8 (Jessie) 上运行良好:
# tcpdump -l -i eth0 | ( gzip -c > test.gz & )
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
tcpdump: Unable to write output: Broken pipe
#
两个问题:
- “显而易见的解决方案”有什么问题?
- 如何在 Debian Jessie 中捕获和压缩 tcpdump 输出? (显而易见的解决方案也不起作用)
谢谢!
【问题讨论】:
-
显而易见的解决方案是在流经管道的内容足够多的情况下工作。如果您只是想捕捉几行,或者需要在按下 ctrl+c 时捕捉到最后的所有内容,那么......是的,您在这里遇到了问题。 (但是,仅捕获非常少量的内容并不是通常需要内联 gzip 的情况)。
-
顺便说一句,对于额外的偏执狂,您可以更改我对
stdbuf -oL tcpdump的回答中的tcpdumps,以告诉glibc 在每一行之后刷新tcpdump 的输出。但是,不应该需要,因为 SIGINT 是可捕获的(因此允许程序有机会自己进行最终刷新)。但是,如果使用 SIGKILL 发生关机,您将需要它。 -
...顺便说一句,BashFAQ #9 可能是有用的补充阅读。
-
感谢您的解释。我刚刚验证过了。
gzip仅在压缩完整的 32k 块时写入文件。由于我最初的tcpdump输出包含许多类似的行,因此需要很长时间才能达到 32k。