summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/pcap.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/pcap.py')
-rw-r--r--scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/pcap.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/pcap.py b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/pcap.py
new file mode 100644
index 00000000..ab4f98a7
--- /dev/null
+++ b/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/pcap.py
@@ -0,0 +1,29 @@
+import os
+from ..trex_stl_packet_builder_scapy import RawPcapReader, RawPcapWriter
+
+
+def __ts_key (a):
+ return float(a[1][0]) + (float(a[1][1]) / 1e6)
+
+def merge_cap_files (pcap_file_list, out_filename, delete_src = False):
+
+ if not all([os.path.exists(f) for f in pcap_file_list]):
+ print("failed to merge cap file list...\nnot all files exist\n")
+ return
+
+ out_pkts = []
+ for src in pcap_file_list:
+ pkts = RawPcapReader(src)
+ out_pkts += pkts
+ if delete_src:
+ os.unlink(src)
+
+ # sort by timestamp
+ out_pkts = sorted(out_pkts, key = __ts_key)
+
+ writer = RawPcapWriter(out_filename, linktype = 1)
+
+ writer._write_header(None)
+ for pkt in out_pkts:
+ writer._write_packet(pkt[0], sec=pkt[1][0], usec=pkt[1][1], caplen=pkt[1][2], wirelen=None)
+