@@ -1188,7 +1188,7 @@ def read_up_to_prompt(self, timeout=60.0, internal_timeout=None,
1188
1188
print_func )[1 ]
1189
1189
1190
1190
def cmd_output (self , cmd , timeout = 60 , internal_timeout = None ,
1191
- print_func = None , safe = False ):
1191
+ print_func = None , safe = False , strip_console_codes = False ):
1192
1192
"""
1193
1193
Send a command and return its output.
1194
1194
@@ -1203,15 +1203,18 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
1203
1203
error messages that make read_up_to_prompt to timeout. Let's
1204
1204
try to be a little more robust and send a carriage return, to
1205
1205
see if we can get to the prompt when safe=True.
1206
-
1206
+ :param strip_console_codes: Whether to remove the escape sequence from the output.
1207
+ In serial sessions, there are escape sequences present. If it is not
1208
+ expected while reading the output remove it by passing
1209
+ serial_console_codes = True.
1207
1210
:return: The output of cmd
1208
1211
:raise ShellTimeoutError: Raised if timeout expires
1209
1212
:raise ShellProcessTerminatedError: Raised if the shell process
1210
1213
terminates while waiting for output
1211
1214
:raise ShellError: Raised if an unknown error occurs
1212
1215
"""
1213
1216
if safe :
1214
- return self .cmd_output_safe (cmd , timeout )
1217
+ return self .cmd_output_safe (cmd , timeout , strip_console_codes )
1215
1218
session_tag = f"[{ self .output_prefix } ] " if self .output_prefix else ""
1216
1219
LOG .debug ("%sSending command: %s" , session_tag , cmd )
1217
1220
self .read_nonblocking (0 , timeout )
@@ -1229,10 +1232,13 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
1229
1232
raise ShellError (cmd , output ) from error
1230
1233
1231
1234
# Remove the echoed command and the final shell prompt
1235
+ if strip_console_codes :
1236
+ # Removing the escape sequence
1237
+ out = astring .strip_console_codes (out )
1232
1238
return self .remove_last_nonempty_line (self .remove_command_echo (out ,
1233
1239
cmd ))
1234
1240
1235
- def cmd_output_safe (self , cmd , timeout = 60 ):
1241
+ def cmd_output_safe (self , cmd , timeout = 60 , strip_console_codes = False ):
1236
1242
"""
1237
1243
Send a command and return its output (serial sessions).
1238
1244
@@ -1244,7 +1250,10 @@ def cmd_output_safe(self, cmd, timeout=60):
1244
1250
:param cmd: Command to send (must not contain newline characters)
1245
1251
:param timeout: The duration (in seconds) to wait for the prompt to
1246
1252
return
1247
-
1253
+ :param strip_console_codes: Whether to remove the escape sequence from the output.
1254
+ In serial sessions, there are escape sequences present. If it is not
1255
+ expected while reading the output remove it by passing
1256
+ serial_console_codes = True.
1248
1257
:return: The output of cmd
1249
1258
:raise ShellTimeoutError: Raised if timeout expires
1250
1259
:raise ShellProcessTerminatedError: Raised if the shell process
@@ -1278,6 +1287,9 @@ def cmd_output_safe(self, cmd, timeout=60):
1278
1287
raise ShellTimeoutError (cmd , out )
1279
1288
1280
1289
# Remove the echoed command and the final shell prompt
1290
+ if strip_console_codes :
1291
+ # Removing the escape sequence
1292
+ out = astring .strip_console_codes (out )
1281
1293
return self .remove_last_nonempty_line (self .remove_command_echo (out ,
1282
1294
cmd ))
1283
1295
0 commit comments