-1

I am trying to execute a command using subprocess.Popen() and extract the result using the following code.

proc = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
gadgets = out.split('\n')

The result is a string and can be split into several lines by splitting based on the newline characters in the result. For simplicity, let's assume the gadgets variable in code snippet is a list of two strings. When I print an individual item from the list on a console using the python print() function, I am getting normal ascii text as follows.

for item in gadgets:
    print(item)

output:

syscall; 
xlatb; ret;

However, when I print the list, I am getting a different kind of characters. The output is as follows:

print(gadgets)

output:

['\x1b[1;33msyscall\x1b[0m\x1b[1;34m; \x1b[0m', '\x1b[1;33mxlatb\x1b[0m\x1b[1;34m; \x1b[0m\x1b[1;33mret\x1b[0m\x1b[1;34m; \x1b[0m']

I believe that the print() function somehow changes the encoding and prints normal ascii text on a console for the individual items. Can anyone please tell me what kind of encoding is used by the print() function in this scenario? Thank you!

Salman Ahmed
  • 141
  • 1
  • 3
  • 14
  • I'm wondering what command produced a list *with* ANSI coloring while its separate items don't have this. I am not sure that is normal behavior. – Jongware Dec 28 '18 at 23:55
  • The command is a custom command. That command produces a set of assembly instructions from machine code. To make things clear, the instructions have a variety of colors. The first output is a colored output, but stack overflow site is not showing the color. Initially, I thought the ANSI escape characters (in the second output) is a part of the assembly instructions. It took me some time to figure out the ANSI escape characters in the second output. – Salman Ahmed Dec 29 '18 at 04:23

1 Answers1

3

The second output has ANSI escape characters for colors. Removing the ANSI escape characters from the second output will provide the first output.

This link [https://stackoverflow.com/a/14693789/3114833] has described the way to remove ANSI escape characters. The following code is copied from the link.

import re

ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
ansi_escape.sub('', sometext)
Salman Ahmed
  • 141
  • 1
  • 3
  • 14