pcap(packet capture,数据包捕获)是保存网络数据常用的一种文件格式。
接下来使用pcap转储器把数据包保存为pcap格式。
import os
from scapy.all import *
pkts = []
count = 0
pcapnum = 0
def write_cap(x):
global pkts
global count
global pcapnum
pkts.append(x)
count += 1
if count == 3:
pcapnum += 1
pname = "pcap{}.pcap".format(pcapnum)
wrpcap(pname, pkts)
pkts = []
count = 0
def test_dump_file():
print("Testing the dump file...")
dump_file = "./pcap1.pcap"
if os.path.exists(dump_file):
print "dump fie {} found.".format(dump_file)
pkts = sniff(offline=dump_file)
count = 0
while (count <=2):
print("----Dumping pkt: {}----".format(count))
print(hexdump(pkts[count]))
count += 1
else:
print("dump fie {} not found.".format(dump_file))
if __name__ == '__main__':
print("Started packet capturing and dumping... Press CTRL+C to exit")
sniff(prn=write_cap)
test_dump_file()