2014-05-09 57 views
1

我發現了很多捕獲數據包並從pcap文件中讀取數據的例子。但沒有任何例子可以捕獲數據包並將其轉儲到pcap中。使用Pcapy將數據包保存到pcap文件

我用下面的代碼here

import pcapy 
from impacket.ImpactDecoder import * 

# list all the network devices 
pcapy.findalldevs() 

max_bytes = 1024 
promiscuous = False 
read_timeout = 100 # in milliseconds 
pc = pcapy.open_live("name of network device to capture from", max_bytes, promiscuous, read_timeout) 

pc.setfilter('tcp') 

# callback for received packets 
def recv_pkts(hdr, data): 
    packet = EthDecoder().decode(data) 
    print packet 

packet_limit = -1 # infinite 
pc.loop(packet_limit, recv_pkts) # capture packets 

任何IDEIA轉儲捕獲的數據包到一個PCAP文件?

回答

3

有一個Dumper對象,你可以從pcapy通過dump_open調用獲得,你可以調用dump。

dumper = pc.dump_open(filename) 
... 
dumper.dump(hrd, data) 
1

Pcapy確實有Dumper對象,但您需要有一個Reader對象來創建它,這是沒有任何意義的。

import pcapy as pc 
dev = pc.lookupdev() 
reader = pc.open_live(dev, 65536, 1, 1) 
dumper = reader.dump_open('pcapy_test.pcap') 

for _ii in range(2): 
    hdr, pkt = reader.next() 
    dumper.dump(hdr, pkt)