From 13afe2306023b1c45e0eb554f9379cc762dcd74f Mon Sep 17 00:00:00 2001 From: Federico Ressi Date: Mon, 16 Aug 2021 10:26:51 +0200 Subject: [PATCH] Disable stdout decoding to stream when reading Tcpdump file Change-Id: Ib9ed0bb6f4c8a2b50daa2a7c05652f55f1f3517e --- tobiko/shell/sh/_execute.py | 24 ++++++++++++++++-------- tobiko/shell/sh/_process.py | 7 +++++++ tobiko/shell/tcpdump/_execute.py | 6 ++++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/tobiko/shell/sh/_execute.py b/tobiko/shell/sh/_execute.py index 3d8b8f71d..3d0a5c800 100644 --- a/tobiko/shell/sh/_execute.py +++ b/tobiko/shell/sh/_execute.py @@ -42,7 +42,7 @@ class ShellExecuteStatus(enum.Enum): def execute_result(command, exit_status=None, timeout=None, status=None, login=None, stdin=None, stdout=None, - stderr=None): + stderr=None, decode_streams=True): command = str(command) if exit_status is not None: exit_status = int(exit_status) @@ -50,9 +50,14 @@ def execute_result(command, exit_status=None, timeout=None, timeout = float(timeout) if status is not None: status = ShellExecuteStatus(status) - stdin = _process.str_from_stream(stdin) - stdout = _process.str_from_stream(stdout) - stderr = _process.str_from_stream(stderr) + if decode_streams: + stdin = _process.str_from_stream(stdin) + stdout = _process.str_from_stream(stdout) + stderr = _process.str_from_stream(stderr) + else: + stdin = _process.bytes_from_stream(stdin) + stdout = _process.bytes_from_stream(stdout) + stderr = _process.bytes_from_stream(stderr) return ShellExecuteResult(command=command, exit_status=exit_status, timeout=timeout, @@ -120,7 +125,7 @@ def _indent(text, space=' ', newline='\n'): def execute(command, environment=None, timeout=None, shell=None, stdin=None, stdout=None, stderr=None, ssh_client=None, - expect_exit_status=0, **kwargs): + expect_exit_status=0, decode_streams=True, **kwargs): """Execute command inside a remote or local shell :param command: command argument list @@ -153,10 +158,12 @@ def execute(command, environment=None, timeout=None, shell=None, return execute_process(process=process, stdin=stdin, login=login, - expect_exit_status=expect_exit_status) + expect_exit_status=expect_exit_status, + decode_streams=decode_streams) -def execute_process(process, stdin, expect_exit_status, login=None): +def execute_process(process, stdin, expect_exit_status, login=None, + decode_streams=True): error = None status = None try: @@ -186,7 +193,8 @@ def execute_process(process, stdin, expect_exit_status, login=None): login=login, stdin=process.stdin, stdout=process.stdout, - stderr=process.stderr) + stderr=process.stderr, + decode_streams=decode_streams) if error: LOG.info("Command error:\n%s\n", result.details) error.result = result diff --git a/tobiko/shell/sh/_process.py b/tobiko/shell/sh/_process.py index e9fc19fae..61e6c90df 100644 --- a/tobiko/shell/sh/_process.py +++ b/tobiko/shell/sh/_process.py @@ -435,6 +435,13 @@ def str_from_stream(stream): return None +def bytes_from_stream(stream): + if stream is not None: + return stream.data + else: + return None + + def default_shell_command(): from tobiko import config CONF = config.CONF diff --git a/tobiko/shell/tcpdump/_execute.py b/tobiko/shell/tcpdump/_execute.py index 71eb12f8b..67f0aade6 100644 --- a/tobiko/shell/tcpdump/_execute.py +++ b/tobiko/shell/tcpdump/_execute.py @@ -65,7 +65,9 @@ def get_pcap(process, ssh_client: ssh.SSHClientType = None) -> dpkt.pcap.Reader: stop_capture(process) - stdout = sh.execute( - f'cat {capture_file}', ssh_client=ssh_client, sudo=True).stdout + stdout = sh.execute(f"cat '{capture_file}'", + ssh_client=ssh_client, + sudo=True, + decode_streams=False).stdout pcap = dpkt.pcap.Reader(io.BytesIO(stdout)) return pcap