Suppressing output of a console application when invoked from Python script [duplicate] - python

This question already has answers here:
Suppress output in Python calls to executables
(9 answers)
Closed 2 years ago.
I'm working on a school project and I have to run John the Ripper through as a Python script.
Currently I'm running the command like this:
subprocess.run(['john --wordlist=/usr/share/john/password.lst --format=sha512crypt /root/Desktop/passwd '], shell=True)
My issue here is that it prints a lot of the process in the command terminal and I can't have anything show up in the terminal.
How can I make it so it doesn't print anything in the terminal?

You want to redirect the print so it dumps it in a file instead of printing on the terminal.
You might want to add something like after your code.
f = open("test.py", "w+")
print(output.stdout,f.write(output.stdout))
If you do cat test.py you should see the output.

Related

How to write log and print to screen at the same time? [duplicate]

This question already has answers here:
logger configuration to log to file and print to stdout
(10 answers)
Closed 2 years ago.
I'm writing a buildscript. I want to log everything that is going on, but I also want to see everything while it is running.
What's the best practice to do this?
I will recommend to use tee command to save output to file
Just write print statement in python script and pipe content to tee command
Tee command capture std in and print and write it to file.
e.g. console content will be saved to build.log
Linux
$ your_buildscript.py 2>&1 | tee build.log
Windows
your_buildscript.py | tee build.log
Refernece:
Linux tee: https://man7.org/linux/man-pages/man1/tee.1.html
windows tee: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/tee-object?view=powershell-7.1

Write shell output into a text file [duplicate]

This question already has an answer here:
How to save the output of an IPython console to a file in Spyder?
(1 answer)
Closed 4 years ago.
How can I write the shell output in to a text file in python? I want to save the shell output as a log in a text file and I am currently using spyder.
Instead of just using print statement or print(statement), use:
a) some logging modules. Have a look at logging module for example.
b) an opened file for the output and write everything to that file. So in your script:
fout=open('log.txt','w')
# Here are your script things....
fout.write(statement+'\n')
# More script things ...
fout.close()
c) use an example from this post: How to save the output of an IPython console to a file in Spyder?

os.system not writing in terminal [duplicate]

This question already has answers here:
Python: How to get stdout after running os.system? [duplicate]
(6 answers)
Closed 6 years ago.
i have a simple test file i created in order to use vmd (a program for my job)
This test file is as simple as :
import os
os.system("vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol")
Basically, im using os.system to launch a program name vmd with another script i wrote and im giving it one argument. What i found it is that when i run this test script, i get nothing done but if i just go in terminal and write :
vmd -eofexit < VMD_script.tcl -args 3spi_cholesterol
everything works perfectly. Is there anything im doing wrong with os.system? I have been using this line for a while now but on linux and it was working perfectly, could it be a mac issue?
Thanks allot
import subprocess
ls_output = subprocess.check_output(['vmd', '-eofexit', '<', 'VMD_script.tcl', '-args', '3spi_cholesterol'])

How to subprocess this call in python: png2pos args > /dev/usb/lp0 [duplicate]

This question already has answers here:
How to redirect output with subprocess in Python?
(6 answers)
Closed 7 years ago.
I currently have the following code:
subprocess.call(["png2pos", "-c", "example_2.png", ">", "/dev/usb/lp0"])
The program png2pos is being accessed because it's giving me the message:
This utility produces binary sequence printer commands. Output have to
be redirected
This is the same error I get if I forget to type in > /dev/usb/lp0, so I'm fairly certain it has something to do with the '>' character. How would one redirect this output to /dev/usb/lp0 with subprocess?
To make sure the output is redirected properly, you need to set shell to True and pass a single string:
subprocess.call("png2pos -c example_2.png > /dev/usb/lp0", shell=True)
Otherwise ">" is treated as a program argument.
I do not have your tool installed, so I cannot really test here. But had an issue with redirecting output from a console application using python before. I had to redirect it using the command itself, not via the shell (as you are trying)
with open("/dev/usb/lp0", 'wb') as output_str:
subprocess.Popen(["png2pos", "-c", "example_2.png"], stdout=output_str)

Python Command Line input? [duplicate]

This question already has answers here:
read subprocess stdout line by line
(10 answers)
Closed 21 days ago.
My Python script will run a bunch of shell scripts that output either 200 SOLUTIONS_REVISION or 400 SOLUTIONS_REVISION when run. The 200 in the output indicates success and the 400 indicates failure.
How can I capture these "returned" strings as strs in the Python code, for further processing?
If you're going to run the command from your Python script then you want to look at subprocess with its stdout arguments. If you're going to run both that script and the Python script from a separate shell script then you want to pipe from one to the next and then read from sys.stdin.

Categories