Py学习  »  Python

如何在Linux上使用管道让tcpdump捕获python read.pcap数据?

shih alex • 4 年前 • 745 次点击  

最近,我设法从我的Raspberry Pi 3b+板捕获TCP数据到本地机器上的Wireshark进行实时处理,但现在我想用python实时捕获“tcp”部分的列“data”,并通过tcp端口将此列的数据发送到另一个监听p的应用程序。端口1234。

我想我试错了。我的尝试如下所示:

下面的make wireshark命令可以实时分析另一台机器的TCP流。

ssh root@10.0.1.2 tcpdump -ns 0 -i eth0 "not port 22" | wireshark -k -i -

它可以工作,Wireshark可以实时工作。我想根据这些命令在终端中转储和显示符合我要求的每个包中的一些数据,因此我尝试筛选我想要的列,开始于:

ssh root@10.0.1.2 tcpdump -ns 0 -i eth0 "not port 22" | python test.py

并将test.py归档,如下所示:

import sys
k = 0
try:
   for line in iter(sys.stdin.readline, b''):
      k = k + 1
      print(line)
except KeyboardInterrupt:
   sys.stdout.flush()
   pass
print(k)

但不能显示任何内容。

顺便说一下,TCP流是以二进制模式传输的。

我想通过tcpdump在tcp数据中实时(或90%实时)捕获一些特定的数据,但我尝试了一些方法,但不起作用。

我还尝试使用命令,使wireshark能够实时分析,然后将数据转发到python脚本,我搜索google 2天,结果为零。

有人能帮我吗?我会非常感激的,谢谢!

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38044
 
745 次点击  
文章 [ 1 ]  |  最新文章 4 年前
al76
Reply   •   1 楼
al76    5 年前

下面是一个简单的例子,说明如何通过管道连接到Python脚本。

import sys
import fileinput


incoming = fileinput.input(sys.argv[1:])
for line in incoming:
    print line.rstrip()

python -u 可能会有帮助

-u     Force  stdin,  stdout  and  stderr to be totally unbuffered.  On
              systems where it matters, also put stdin, stdout and  stderr  in
              binary  mode.   Note  that there is internal buffering in xread‐
              lines(), readlines() and file-object  iterators  ("for  line  in
              sys.stdin")  which  is  not  influenced by this option.  To work
              around this, you will want to use "sys.stdin.readline()"  inside
              a "while 1:" loop.