1*a90b9d01SCy Schubert#!/usr/bin/env python3 2f05cddf9SRui Paulo# 3*a90b9d01SCy Schubert# Copyright (c) 2012-2022, Intel Corporation 4f05cddf9SRui Paulo# 5f05cddf9SRui Paulo# Author: Johannes Berg <johannes@sipsolutions.net> 6f05cddf9SRui Paulo# 7f05cddf9SRui Paulo# This software may be distributed under the terms of the BSD license. 8f05cddf9SRui Paulo# See README for more details. 9f05cddf9SRui Paulo 10f05cddf9SRui Pauloimport sys, struct, re 11*a90b9d01SCy Schubertfrom binascii import unhexlify 12f05cddf9SRui Paulo 13f05cddf9SRui Paulodef write_pcap_header(pcap_file): 14f05cddf9SRui Paulo pcap_file.write( 15f05cddf9SRui Paulo struct.pack('<IHHIIII', 16f05cddf9SRui Paulo 0xa1b2c3d4, 2, 4, 0, 0, 65535, 17f05cddf9SRui Paulo 105 # raw 802.11 format 18f05cddf9SRui Paulo )) 19f05cddf9SRui Paulo 20f05cddf9SRui Paulodef pcap_addpacket(pcap_file, ts, data): 21f05cddf9SRui Paulo # ts in seconds, float 22f05cddf9SRui Paulo pcap_file.write(struct.pack('<IIII', 23f05cddf9SRui Paulo int(ts), int(1000000 * ts) % 1000000, 24f05cddf9SRui Paulo len(data), len(data))) 25f05cddf9SRui Paulo pcap_file.write(data) 26f05cddf9SRui Paulo 27f05cddf9SRui Pauloif __name__ == "__main__": 28f05cddf9SRui Paulo try: 29f05cddf9SRui Paulo input = sys.argv[1] 30f05cddf9SRui Paulo pcap = sys.argv[2] 31f05cddf9SRui Paulo except IndexError: 324bc52338SCy Schubert print("Usage: %s <log file> <pcap file>" % sys.argv[0]) 33f05cddf9SRui Paulo sys.exit(2) 34f05cddf9SRui Paulo 35f05cddf9SRui Paulo input_file = open(input, 'r') 36*a90b9d01SCy Schubert pcap_file = open(pcap, 'wb') 37f05cddf9SRui Paulo frame_re = re.compile(r'(([0-9]+.[0-9]{6}):\s*)?nl80211: MLME event frame - hexdump\(len=[0-9]*\):((\s*[0-9a-fA-F]{2})*)') 38f05cddf9SRui Paulo 39f05cddf9SRui Paulo write_pcap_header(pcap_file) 40f05cddf9SRui Paulo 41f05cddf9SRui Paulo for line in input_file: 42f05cddf9SRui Paulo m = frame_re.match(line) 43f05cddf9SRui Paulo if m is None: 44f05cddf9SRui Paulo continue 45f05cddf9SRui Paulo if m.group(2): 46f05cddf9SRui Paulo ts = float(m.group(2)) 47f05cddf9SRui Paulo else: 48f05cddf9SRui Paulo ts = 0 49f05cddf9SRui Paulo hexdata = m.group(3) 50f05cddf9SRui Paulo hexdata = hexdata.split() 51*a90b9d01SCy Schubert data = unhexlify("".join(hexdata)) 52f05cddf9SRui Paulo pcap_addpacket(pcap_file, ts, data) 53f05cddf9SRui Paulo 54f05cddf9SRui Paulo input_file.close() 55f05cddf9SRui Paulo pcap_file.close() 56