Created
February 13, 2020 16:01
-
-
Save angely-dev/04c17ec10a03a2f15085cd1335147ff4 to your computer and use it in GitHub Desktop.
Convert Huawei capture-packet output to Wireshark.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import remove | |
from re import search | |
from scapy.all import * | |
import argparse | |
# Usage | |
parser = argparse.ArgumentParser(description='Convert raw Huawei hex packets to cap file.') | |
parser.add_argument('--raw-file', help='the raw file location (source)', default='raw.txt') | |
parser.add_argument('--cap-file', help='the cap file location (destination)', default='cap.pcap') | |
args = parser.parse_args() | |
# Delete previous cap file | |
try: | |
remove(args.cap_file) | |
except FileNotFoundError: | |
pass | |
# Extract each frame from raw file | |
with open(args.raw_file) as file: | |
frames = file.read().split('-------------------------------------------------------') | |
# Convert each frame to hex bytes | |
valid_frames = [] | |
for frame in frames: | |
frame = frame.replace('-', '') | |
frame = frame.replace('\n', '') | |
frame = frame.replace(' ', '') | |
if frame.isalnum(): | |
# Frame is considered valid | |
# if the raw file is correctly formatted (see below) | |
# all frames are valid | |
valid_frames.append(frame) | |
# Write pcap file | |
for frame in valid_frames: | |
frame = hex_bytes(frame) | |
wrpcap(args.cap_file, frame, append=True) | |
# Print summary | |
print(f'OK - {len(valid_frames)} frames written') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note this is really Huawei specific (an example from the official doc).
Example of a
raw.txt
file (there can be more packets, e.g., 100 is common).Example of usage:
It generates a
cap.pcap
file which can be opened with Wireshark: