【ALLBUS-TAP】python解析pcapng文件-分析报文实时性

之前的文章中描述了如何通过wireshark手工导出allbus-tap的报文时间戳,对于批量的文件不太方便,下文介绍一种使用脚本解析pcapng的方法,可以快速把报文中的时间戳信息导出到csv文件中。

代码如下:

import pyshark
import pandas as pd
from datetime import datetime

pcapng_file = "3.pcapng"
capture=pyshark.FileCapture(pcapng_file)

PKT_MAX_NUM = 20000

timestamp_old = 0
warning_list = []
data_list = []
idx = 0
timestamp_first = 0
for pkt in capture:
    if idx > PKT_MAX_NUM:
        break
    layer         = pkt.get_multiple_layers("TAP")
    if layer == []:
        warning_list.append(["index:%d Layer Error Detected"%idx])
        continue
    length        = pkt.length
    dst_mac       = pkt.ETH.dst
    src_mac       = pkt.ETH.src
    type          = pkt.ETH.type
    timestamp_ns  = int(pkt.TAP.timestamp,16)
    fcs           = pkt.TAP.fcs 
    channel       = pkt.TAP.channel
    port          = "X1.2" if pkt.TAP.port == "0x00000040" else "X1.1"
    recv_err      = pkt.TAP.rxerror
    align_err     = pkt.TAP.alignerror
    crc_err       = pkt.TAP.crcerror
    if idx == 0:
        timestamp_first = timestamp_ns
    timestamp_ns  = timestamp_ns - timestamp_first
    if(idx % 100 == 0):
        print("processing....",idx)

    data_list.append([idx,
                     length,
                     src_mac,
                     dst_mac,
                     type,
                     timestamp_ns , 
                     fcs          , 
                     channel      , 
                     port         , 
                     recv_err     , 
                     align_err    , 
                     crc_err      ] )
    idx           += 1

time_str = str(datetime.now().strftime("%Y_%m_%d_%H_%M_%S"))
output_file = time_str + "_allbus_parser.csv"

for x in warning_list:
    print(x)

df = pd.DataFrame(data_list,columns=[ 
                                "Index",
                                "Length",
                                "Source MAC",
                                "Dest MAc" ,
                                "Eth Type" ,
                                "Timestamp(ns)" ,
                                "FCS"           ,
                                "Channel"       ,
                                "Port"          ,
                                "Receive Error" ,
                                "Alignment Error",
                                "Crc Error"      ])

df.to_csv(output_file,index=False)

导出的csv文件即可通过excel进行画图分析实时性,当然有兴趣也可以通过python直接绘图

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值