1

I'm attempting to automate PsExec using a python script. I'd like to be able to read computer names to access and automatically run commands on all of them while getting the output. Is there any python library or other application that will allow me to run an application while still automatically giving input and getting output?

1

The standard python library subprocess has popen and since 3.5 run both of which support piping to stdin and from both stdout and stderr to do this. Currently run is the recommended use where it can handle the required functionality. See the documents for details.

Be sure to read the Frequently Used Arguments section carefully. If there is expected to be a lot of output it is often better to use setting stdout &/or stderr to a writable open file handle to avoid possible deadlocks or memory overflow.

If you want to carry on processing while an external program executes (possibly controlling input & output) use asyncio.create_subprocess_exec in python > 3.5.

You might also wish to take a look at GRR Rapid Response which will allow a more pythonic method of doing similar tasks.

1

You might be able to get the output of a command by redirecting output to a file and reading that file. The following should work on most platforms:

from os import system
system("yourcommand 1> logfile.txt 2>&1") # add " &" (without quotes) to the end of the command string to skip waiting for execution to complete. Note that you'll need to check if your process is complete by using some other method.
result = open("logfile.txt", "r").read()

See here for additional information: https://stackoverflow.com/questions/1420965/redirect-windows-cmd-stdout-and-stderr-to-a-single-file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.