Skip to content

Commit

Permalink
Set utf-8 to be default charset when can't decode with defined charse…
Browse files Browse the repository at this point in the history
…t and handle exceptions
  • Loading branch information
L2JE committed Feb 23, 2023
1 parent aa10a6f commit c12f75a
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions contents/winrm-exec.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down

0 comments on commit c12f75a

Please sign in to comment.