log2pcap.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (c) 2012, Intel Corporation
  4. #
  5. # Author: Johannes Berg <johannes@sipsolutions.net>
  6. #
  7. # This software may be distributed under the terms of the BSD license.
  8. # See README for more details.
  9. import sys, struct, re
  10. def write_pcap_header(pcap_file):
  11. pcap_file.write(
  12. struct.pack('<IHHIIII',
  13. 0xa1b2c3d4, 2, 4, 0, 0, 65535,
  14. 105 # raw 802.11 format
  15. ))
  16. def pcap_addpacket(pcap_file, ts, data):
  17. # ts in seconds, float
  18. pcap_file.write(struct.pack('<IIII',
  19. int(ts), int(1000000 * ts) % 1000000,
  20. len(data), len(data)))
  21. pcap_file.write(data)
  22. if __name__ == "__main__":
  23. try:
  24. input = sys.argv[1]
  25. pcap = sys.argv[2]
  26. except IndexError:
  27. print "Usage: %s <log file> <pcap file>" % sys.argv[0]
  28. sys.exit(2)
  29. input_file = open(input, 'r')
  30. pcap_file = open(pcap, 'w')
  31. 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})*)')
  32. write_pcap_header(pcap_file)
  33. for line in input_file:
  34. m = frame_re.match(line)
  35. if m is None:
  36. continue
  37. if m.group(2):
  38. ts = float(m.group(2))
  39. else:
  40. ts = 0
  41. hexdata = m.group(3)
  42. hexdata = hexdata.split()
  43. data = ''.join([chr(int(x, 16)) for x in hexdata])
  44. pcap_addpacket(pcap_file, ts, data)
  45. input_file.close()
  46. pcap_file.close()