Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pyshark-如何在livecapture期间打印目标ip?_Python_Python 3.x_Wireshark_Pyshark - Fatal编程技术网

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

Python pyshark-如何在livecapture期间打印目标ip?,python,python-3.x,wireshark,pyshark,Python,Python 3.x,Wireshark,Pyshark,我是pyshark的新手。 我试图在终端打印“目的地ip”,该ip来自使用udp过滤器的livecapture期间的握手数据包。(蟒蛇3) 我在几个小时内找不到有用的东西,所以这是我最后的选择。 这是我的尝试 import pyshark file = "C:/Users/S0B0/Desktop/capture/output6" + ".cap" output = open(file, "w") time = 86399 cap

我是pyshark的新手。 我试图在终端打印“目的地ip”,该ip来自使用udp过滤器的livecapture期间的握手数据包。(蟒蛇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上有一个名为的文档和代码示例,您可能会发现它很有用。

谢谢您的回答!这对我很有帮助,我用你们的例子@S0B0做了我想做的,不客气。我很高兴能帮上忙。查看我编写的文档和pyshark的代码示例。