PCAP 文件库¶
介绍¶
OS-Ken PCAP 文件库帮助您读取/写入 PCAP 文件,该文件格式在 Wireshark Wiki 中描述。
读取 PCAP 文件¶
要加载 PCAP 文件中包含的数据包数据,您可以使用 pcaplib.Reader。
- class os_ken.lib.pcaplib.Reader(file_obj)¶
PCAP 文件读取器
参数
描述
file_obj
以二进制模式读取 PCAP 文件的文件对象
用例示例
from os_ken.lib import pcaplib from os_ken.lib.packet import packet frame_count = 0 # iterate pcaplib.Reader that yields (timestamp, packet_data) # in the PCAP file for ts, buf in pcaplib.Reader(open('test.pcap', 'rb')): frame_count += 1 pkt = packet.Packet(buf) print("%d, %f, %s" % (frame_count, ts, pkt))
写入 PCAP 文件¶
要转储您的 OSKenApp 接收到的数据包数据,您可以使用 pcaplib.Writer。
- class os_ken.lib.pcaplib.Writer(file_obj, snaplen=65535, network=1)¶
PCAP 文件写入器
参数
描述
file_obj
以二进制模式写入 PCAP 文件的文件对象
snaplen
捕获数据包的最大长度(以字节为单位)
network
数据链路类型。(例如,以太网为 1,有关详细信息,请参阅 tcpdump.org)
用例示例
... from os_ken.lib import pcaplib class SimpleSwitch13(app_manager.OSKenApp): OFP_VERSIONS = [ofproto_v1_3.OFP_VERSION] def __init__(self, *args, **kwargs): super(SimpleSwitch13, self).__init__(*args, **kwargs) self.mac_to_port = {} # Create pcaplib.Writer instance with a file object # for the PCAP file self.pcap_writer = pcaplib.Writer(open('mypcap.pcap', 'wb')) ... @set_ev_cls(ofp_event.EventOFPPacketIn, MAIN_DISPATCHER) def _packet_in_handler(self, ev): # Dump the packet data into PCAP file self.pcap_writer.write_pkt(ev.msg.data) ...