From c12f75a874fc3542e8c97d87f09b1fa61b67e8f9 Mon Sep 17 00:00:00 2001 From: L2JE Date: Thu, 23 Feb 2023 10:52:57 -0300 Subject: [PATCH] Set utf-8 to be default charset when can't decode with defined charset and handle exceptions --- contents/winrm-exec.py | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) mode change 100644 => 100755 contents/winrm-exec.py diff --git a/contents/winrm-exec.py b/contents/winrm-exec.py old mode 100644 new mode 100755 index 4faa9d8..75df1d9 --- a/contents/winrm-exec.py +++ b/contents/winrm-exec.py @@ -209,7 +209,8 @@ def filter(self, record): else: krbdelegation = False -output_charset = "utf-8" +DEFAULT_CHARSET = 'utf-8' +output_charset = DEFAULT_CHARSET if "RD_NODE_OUTPUT_CHARSET" in os.environ: output_charset = os.getenv("RD_NODE_OUTPUT_CHARSET") @@ -300,15 +301,23 @@ def filter(self, record): while True: t.join(.1) - if sys.stdout.tell() != lastpos: - sys.stdout.seek(lastpos) - read=sys.stdout.read() - if isinstance(read, str): - realstdout.write(read) - else: - realstdout.write(read.decode(output_charset)) - - lastpos = sys.stdout.tell() + try: + if sys.stdout.tell() != lastpos: + sys.stdout.seek(lastpos) + read=sys.stdout.read() + if isinstance(read, str): + realstdout.write(read) + else: + realstdout.write(read.decode(output_charset)) + except UnicodeDecodeError: + try: + realstdout.write(read.decode(DEFAULT_CHARSET)) + except Exception as e: + log.error(e) + except Exception as e: + log.error(e) + + lastpos = sys.stdout.tell() if not t.is_alive(): break @@ -318,8 +327,6 @@ def filter(self, record): sys.stdout = realstdout sys.stderr = realstderr - - if exitBehaviour == 'console': if tsk.e_std: log.error("Execution finished with the following error")