pyshark使用教程

安装

pip install pyshark

使用

例如:分析现有的pcap文件:

import pyshark

pcap = pyshark.FileCapture("test1.pcap", tshark_path="/Applications/Wireshark.app/Contents/MacOS/tshark")

两个参数分别指定输入文件和 tshark 路径

然后,就可以使用循环遍历pcap文件(也可以使用下标):

for p in pcap:
    print(p)

输出的结构和wireshark看到的保持一致,结果如下:
在这里插入图片描述
如果想单独看IP层或者TCP or UDP层的话,只需要:

print(pcap[0].ip)

输出:
在这里插入图片描述
以IP层为例,如果想单独提取其中的某个参数:

print(pcap[0].ip.src)
print(pcap[0].ip.ttl)
print(pcap[0].ip.version)
print(pcap[0].ip.proto)

输出如下:
在这里插入图片描述
如何看pcap对象有哪些可用的字段?对pcap对象使用 dir() 函数即可

print(dir(pcap[0]))

结果如下:
在这里插入图片描述
同理,如何看某一层有哪些可用的字段?以IP层为例:

print(dir(pcap[0].ip))

输出如下:
在这里插入图片描述

总结

日常使用的话还可以,但速度比较一般

猜你喜欢

转载自blog.csdn.net/airenKKK/article/details/124993912