pyshark-如何在直播过程中打印目的地ip



我是pyshark的新手。我正试图在终端中打印";目的地ip";在使用udp过滤器进行实时捕获期间,从握手数据包中提取。(蟒蛇3(

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

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数据包分析的文档和代码示例,您可能会发现它很有用。

最新更新