summaryrefslogtreecommitdiffstats
path: root/scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/pcap.py
diff options
context:
space:
mode:
authorimarom <imarom@cisco.com>2016-03-23 09:19:46 +0200
committerimarom <imarom@cisco.com>2016-03-23 13:01:08 +0200
commit5e727474efe18b3800df6130c068e668fadd2e0b (patch)
tree1c9bce9e6367b892830df6a49798cd14bbe434d8 /scripts/automation/trex_control_plane/stl/trex_stl_lib/utils/pcap.py
parent46f110d0c728ba9299156cf92a59c27c2d0348fa (diff)
Python 3 - another drop (package fixups)
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)
+