当前位置:网站首页>Using producer consumer model and dpkt to process pcap files
Using producer consumer model and dpkt to process pcap files
2022-07-26 07:53:00 【lepton126】
1、 Use find Command to generate pcap File list file
2、 Use dpkt extract pcap Source address source port destination address destination port , The printing program takes time
find route -type f -name *.pcap >pcaplist.txt
#!/bin/env python3
import time
from multiprocessing import Process,JoinableQueue,cpu_count
import dpkt
import datetime
from dpkt.utils import mac_to_str,inet_to_str
# send dpkt extract pcap Information about each in the document
def print_packets(pcap):
for timestamp,buf in pcap:
print('Timestamp: ',str(datetime.datetime.utcfromtimestamp(timestamp)))
eth = dpkt.ethernet.Ethernet(buf)
print('Ethernet Frame: ',mac_to_str(eth.src),mac_to_str(eth.dst),eth.type)
if not isinstance(eth.data,dpkt.ip.IP):
print('Non IP Packet type not supported %s\n'% eth.data.__class__.__name__)
continue
ip = eth.data
print('IP: %s -> %s (len=%d ttl=%d DF=%d MF=%d offset=%d)\n' %(inet_to_str(ip.src),inet_to_str(ip.dst),ip.len,ip.ttl,ip.df,ip.mf,ip.offset))
print('**Pretty print demo **\n')
eth.pprint()
# Will read pcapfile file name , Write the file name to be processed to result_f
def analyze_pcap(pcapfile,result_f):
pcapfile=pcapfile.strip()
with open(pcapfile,'rb') as pcapfilef:
pcap = dpkt.pcap.Reader(pcapfilef) # call dpkt, Read pcap file
print_packets(pcap) # Function function , Print relevant information here , It can be replaced by other functions
result_f.write(pcapfile) # After processing , To be dealt with pcap Write a file name , As log Record
result_f.flush()# This must have , Save to hard disk
# A consumer function , Read the file list of the files to be processed
def consumer(queue,result_f):
while True:
pcapfile = queue.get()
analyze_pcap(pcapfile,result_f)
queue.task_done()
# A producer function , Read the file name from the list of files to be processed , Put in queue
def producer(queue):
with open('pcapfilelist.txt','r') as f:
for pcapfilename in f:
queue.put(pcapfilename)
def main():
with open('processed_pcapfile.txt','w+') as result_f:
queue = JoinableQueue(40) # Limit the number of queues
pc = Process(target=producer,args=(queue,))
pc.start()
for _ in range(cpu_count()):
c1 = Process(target=consumer,args=(queue,result_f))
c1.daemon = True
c1.start()
pc.join()
queue.join()
if __name__ == '__main__':
now = lambda:time.time()
start = now()
main()
print("Time:",now() - start)
边栏推荐
- Unity metaverse (II), mixamo & animator hybrid tree and animation fusion
- 2019 ZTE touyue · model compression scheme
- shardingjdbc踩坑记录
- 程序环境和预处理
- Regression analysis code implementation
- MMOE多目标建模
- "Door lock" ignites a heated discussion on the safety of living alone. The new poster picture is suffocating
- 时间序列分析预测实战之ARIMA模型
- C language keyword extern
- NLP natural language processing - Introduction to machine learning and natural language processing (3)
猜你喜欢

Parameterization of JMeter performance test using CSV file

Master slave database deployment

JMeter性能测试之使用CSV文件参数化

【推荐系统经典论文(十)】阿里SDM模型

Kdd2022 | uncover the mystery of Kwai short video recommendation re ranking, and recommend the new SOTA

Open source management system based on ThinkPHP

From boosting to lamdamart

Summary of distributed related interview questions

Audio and video learning (10) -- PS streaming

20220209 create a basic Servlet
随机推荐
[uniapp] encapsulation of multiple payment methods
Devaxpress.xtraeditors.datanavigator usage
OVS underlying implementation principle
From boosting to lamdamart
C language keyword extern
Shardingjdbc pit record
How to close the high-level port
Regression analysis code implementation
模型剪枝三:Learning Structured Sparsity in Deep Neural Networks
时间序列分析预测实战之ARIMA模型
Keras learning part: obtaining the output results of neural network middle layer
Leetcode 206. reverse chain list (2022.07.25)
Jmeter性能测试之将每次接口请求的结果保存到文件中
The analysis, solution and development of the problem of router dropping frequently
Learning Efficient Convolutional Networks Through Network Slimming
C# 使用Log4Net记录日志(基础篇)
Speech at 2021 global machine learning conference
Comparison and difference between dependence and Association
JWT快速入门
机器学习相关比赛网站