I need to issue some commands to the currently executing program and get the output of the current (remotely) executing program into some string in the script. The problem I am currently encountering is that I don't know the output of each command which output can also vary as it can be read from user.
e.g.
and if i run the commands manually terminal will get something like this
bash$ ./my_programe
my_program: print_output_1
my_program:first_line_printed_with_unknown_length
my_program: print_output_2
my_program:second_line_printed_with_unknown_length
my_program: exit
bash$
so i should get "first_line_printed_with_unknown_length" and "second_line_printed_with_unknown_length" in a python string like
execute(my_program)
str1 = execute( print_output_1 )
str2 = execute( print_output_2 )
val = execute( exit )
You can make use of the subprocess module to execute external commands. It is best to start with much simpler commands first to get the gist of it all. Below is a dummy example:
import subprocess
from subprocess import PIPE
def main():
process = subprocess.Popen('echo %USERNAME%', stdout=PIPE, shell=True)
username = process.communicate()[0]
print username #prints the username of the account you're logged in as
if __name__ == '__main__':
main()
This will grab the output from echo %USERNAME%
and store it. Pretty simply to give you the general idea.
From the aforementioned documentation:
Warning: Using shell=True can be a security hazard. See the warning under Frequently Used Arguments for details.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments