如何使用tcpdump和滑动窗口或类似“logrotate”捕获stream量?

我想用tcpdump捕获一些stream量来排除故障。 问题是,错误是不可重现的。 为了不用填充孔盘,我想用某种滑动窗口捕捉交通。

假设我将捕获写入文件,当文件达到1GB大小时,将丢弃最旧的数据包并写入新的数据包。 这样我只能得到几个小时的stream量,但希望足够有正确的数据包时,用户的电话。

我找不到tcpdump的选项。 有人有一个想法如何解决这个问题?

-c选项可以帮助你:

-c Exit after receiving count packets. 

所以这会得到一个循环的traffic.dmp文件:

 while : do tcpdump -i eth0 -c 50000 -C 1 -w traffic.dmp done 

如果你把它放在for循环中,你可能会得到一系列的文件:

 for file in 1 2 3 4 5 do tcpdump -i eth0 -c 50000 -C 1 -w traffic${file}.dmp done 

。 在找出某个数字不大的数字后,只需调整数字即可捕获数小时的数据包。

-C也看起来有趣:

  -C Before writing a raw packet to a savefile, check whether the file is currently larger than file_size and, if so, close the current savefile and open a new one. Savefiles after the first savefile will have the name specified with the -w flag, with a number after it, starting at 1 and continuing upward. The units of file_size are millions of bytes (1,000,000 bytes, not 1,048,576 bytes). 

如果你坚持使用tcpdump,davey的答案是正确的。 但是,还有其他捕获包,生成pcap文件,为这类工作提供更多的选项。 让我们来提一下:

  • tshark是Wireshark程序的一部分。 它的-a (“停止写入一个捕获文件,达到值的千字节大小”)和-b (“当第一个捕获文件填满,TShark将切换写入下一个文件等)”选项似乎特别有趣

  • pcapdump, pcaputils包的一部分。 请参阅configuration选项interval= (捕获N秒后移至下一个文件)和filefmt= (用于生成捕获文件名称的模式)。