pyshark - 如何在 livecapture 期间打印目标 ip?

pyshark - How can I print destination ip during livecapture?

我是pyshark的新手。 在使用 udp 过滤器进行实时捕获期间,我试图在终端中打印来自握手数据包的“目标 ip”。 (python3)

我在几个小时内找不到有用的东西,所以这是我最后的选择。 这是我的尝试。

import pyshark

file = "C:/Users/S0B0/Desktop/capture/output6" +  ".cap"
output = open(file, "w")
time = 86399

capture = pyshark.LiveCapture(interface="Ethernet",bpf_filter="udp",output_file=file,only_summaries=True)
capture.set_debug()
capture.sniff(timeout=time)

for p in capture:
    if hasattr(p, 'udp'):
        print(p.udp.srcport + ' -- ' + p.udp.dstport)

output.close()

这是你想要做的吗?

capture = pyshark.LiveCapture(interface="en0")
capture.set_debug()
for packet in capture.sniff_continuously():
if hasattr(packet, 'udp'):
    protocol = packet.transport_layer
    source_address = packet.ip.src
    source_port = packet[packet.transport_layer].srcport
    destination_address = packet.ip.dst
    destination_port = packet[packet.transport_layer].dstport
    print(f'{protocol}  {source_address}:{source_port} --> {destination_address}:{destination_port}')

我在 GitHub 上有一个名为 pyshark packet analysis 的文档和代码示例,您可能会觉得有用。