简体   繁体   English

在Linux上实时捕获数据包

[英]Live packet capture on linux

I would like to know if there is any possibility to live capture network packets and save it to a variable for example in python. 我想知道是否有可能实时捕获网络数据包并将其保存到例如python中的变量中。 I need some information to get from it and not to save it to a file. 我需要一些信息来获取它,而不是将其保存到文件中。

I need to capture http packets and get source addres and its content, which should be a html code, to extract only text from it and then do the rest of the job on that information. 我需要捕获http数据包并获取源地址及其内容(应该是html代码),以从中提取文本,然后对该信息进行其余的工作。 There is no reason to save every packet to a file because whole process would be more slower. 没有必要将每个数据包保存到文件中,因为整个过程会更慢。 I was looking for quite a long time for any tool to do this but no success. 我一直在寻找很长一段时间的任何工具来做到这一点,但没有成功。 Please, if you know any tool that could help me to do this, write about it. 请,如果您知道有什么工具可以帮助我做到这一点,请写下它。

I'm sure you have seen this link about a library to process PCAP files. 我确定您已经看过有关处理PCAP文件的库的链接 Now the question is how to acquire in real time without storing in a file. 现在的问题是如何实时获取而不存储在文件中。

Probably easiest is to use a fifo 可能最简单的方法是使用FIFO

$ mkfifo /tmp/tcpdump.fifo

Now you can capture and feed data into the named fifo 现在您可以捕获数据并将其输入到名为fifo的数据中

$ sudo tcpdump -s0 -i eth0 -f /tmp/tcpdump.fifo tcp port 80

And in your python program you can open '/tmp/tcpdump.fifo' as the input file as per the instructions in the link. 并且在您的python程序中,您可以按照链接中的说明打开“ /tmp/tcpdump.fifo”作为输入文件。

Alternatively you can try opening '/dev/stdin' in your program and reading the data from there; 另外,您可以尝试在程序中打开'/ dev / stdin'并从那里读取数据。 you could then pipe the PCAP data straight into stdin using the shell and skipping the intermediate named fifo. 然后,您可以使用Shell并跳过名为fifo的中间件,将PCAP数据直接传递到stdin中。

$ sudo tcpdump -s0 -i eth0 -f - tcp port 80 | ./youprogram.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM