@@ -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,19 @@ 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
1215
+
1212
1216
"""
1213
1217
if safe :
1214
- return self .cmd_output_safe (cmd , timeout )
1218
+ return self .cmd_output_safe (cmd , timeout , strip_console_codes )
1215
1219
session_tag = f"[{ self .output_prefix } ] " if self .output_prefix else ""
1216
1220
LOG .debug ("%sSending command: %s" , session_tag , cmd )
1217
1221
self .read_nonblocking (0 , timeout )
@@ -1229,10 +1233,13 @@ def cmd_output(self, cmd, timeout=60, internal_timeout=None,
1229
1233
raise ShellError (cmd , output ) from error
1230
1234
1231
1235
# Remove the echoed command and the final shell prompt
1236
+ if strip_console_codes :
1237
+ # Removing the escape sequence
1238
+ out = astring .strip_console_codes (out )
1232
1239
return self .remove_last_nonempty_line (self .remove_command_echo (out ,
1233
1240
cmd ))
1234
1241
1235
- def cmd_output_safe (self , cmd , timeout = 60 ):
1242
+ def cmd_output_safe (self , cmd , timeout = 60 , strip_console_codes = False ):
1236
1243
"""
1237
1244
Send a command and return its output (serial sessions).
1238
1245
@@ -1244,7 +1251,10 @@ def cmd_output_safe(self, cmd, timeout=60):
1244
1251
:param cmd: Command to send (must not contain newline characters)
1245
1252
:param timeout: The duration (in seconds) to wait for the prompt to
1246
1253
return
1247
-
1254
+ :param strip_console_codes: Whether to remove the escape sequence from the output.
1255
+ In serial sessions, there are escape sequences present. If it is not
1256
+ expected while reading the output remove it by passing
1257
+ serial_console_codes = True.
1248
1258
:return: The output of cmd
1249
1259
:raise ShellTimeoutError: Raised if timeout expires
1250
1260
:raise ShellProcessTerminatedError: Raised if the shell process
@@ -1278,6 +1288,9 @@ def cmd_output_safe(self, cmd, timeout=60):
1278
1288
raise ShellTimeoutError (cmd , out )
1279
1289
1280
1290
# Remove the echoed command and the final shell prompt
1291
+ if strip_console_codes :
1292
+ # Removing the escape sequence
1293
+ out = astring .strip_console_codes (out )
1281
1294
return self .remove_last_nonempty_line (self .remove_command_echo (out ,
1282
1295
cmd ))
1283
1296
0 commit comments