報文數據的txt文件 轉換成wireshark可以識別的k12文件

implement a python app which can covert hex raw packet data to k12 file.

Wireshark can open the k12 file and parse the packet content.

 

#########1.txt###########

09002b00 00050020 8fca980d 8100000a 05d4fefe 03831401 06110100 03020110
11011011 005a05d1 0081018e 01040349 0001f00f 01008000 04022022 02202200
800006d3 03000000 e810fe80 00000000 00000220 8ffffeca 980d08ff 00000000
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000

 

# ecoding=utf-8
import re
def pktfile_raw_2_k12(infile, outfile):
    infile = open(ifn,'rb')
    outfile = open(ofn,'wb')
    all = "" 
    for eachline in infile.readlines():
        #去掉文本行裏面的空格 換行 \t(其他有要去除的也可以放到' \r\n\t'裏面)
        lines = filter(lambda ch: ch not in ' \r\n\t', eachline)
        all=all+lines 
    # 寫出正則表達式 任意2個字符
    pattern = re.compile('.{2}')
    # findall是找到所有的字符,再在字符中添加'|',當然你想添加其他東西當然也可以
    modified_pkt_line = '|'.join(pattern.findall(all))
    modified_pkt_line = '|'+ modified_pkt_line + '|'
    k12_headline = """
    +---------+---------------+----------+
    02:43:09,047,121   ETHER
    |0   """
    k12_pkt_string = k12_headline + modified_pkt_line + "\r\n"+"\r\n"
    outfile.write(k12_pkt_string) 
    infile.close
    outfile.close

ifn = r"1.txt"
ofn = r"1_k12.txt"
pktfile_raw_2_k12(ifn, ofn)

 

#########1_k12.txt###########


+---------+---------------+----------+
02:43:09,047,121   ETHER
|0   |09|00|2b|00|00|05|00|20|8f|ca|98|0d|81|00|00|0a|05|d4|fe|fe|03|83|14|01|06|11|01|00|03|02|01|10|11|01|10|11|00|5a|05|d1|00|81|01|8e|01|04|03|49|00|01|f0|0f|01|00|80|00|04|02|20|22|02|20|22|00|80|00|06|d3|03|00|00|00|e8|10|fe|80|00|00|00|00|00|00|02|20|8f|ff|fe|ca|98|0d|08|ff|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|00|


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章