0

I'm trying to get data from a command in ubuntu's terminal and use it in python. when I print the output in python, I get the right thing (part of the data):

[+] Boot and services
------------------------------------
- Service Manager [ systemd ]
- Checking UEFI boot [ DISABLED ]
- Checking presence GRUB2 [ FOUND ]
- Checking for password protection [ NONE ]
- Check running services (systemctl) [ DONE ]
Result: found 35 running services

But when I'm saving it, in a variable\ to a file\ send the output right to text file (with > in terminal) I'm getting this:

[+] [1;33mBoot and services[0m
------------------------------------
[2C- Service Manager[42C [ [1;32msystemd[0m ]
[2C- Checking UEFI boot[39C [ [1;37mDISABLED[0m ]
[2C- Checking presence GRUB2[34C [ [1;32mFOUND[0m ]
[4C- Checking for password protection[23C [ [1;31mNONE[0m ]
[2C- Check running services (systemctl)[23C [ [1;32mDONE[0m ]
[8CResult: found 35 running services[20C

This is supposed to be the same.

the python code that I'm using:

output_lynis = subprocess.getoutput(to_execute)
print(output_lynis)

printing it is ok, but saving it to a variable\ file ruins it. My question is obviously, how can I save it correctly? (I'm running lynis in ubuntu and trying to save the output)

  • 1
    Those extra characters tell your terminal how to format and display the text. In order to remove them you'll have to sanitize the output of escape codes. For example 1;33m tells the terminal to display the header bold and yellow. See [here](https://tldp.org/LDP/abs/html/colorizing.html) for more. – 0x263A Sep 09 '21 at 13:22

0 Answers0