Python script to execute external commands

joseph

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.

  1. ./my_program
  2. print_output_1 [ with in my_program ]
  3. print_output_2 [ with in my_program ]
  4. exit [ with in my_program ]

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 )
Juxhin

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to execute multiple CLI commands in a python script?

From Dev

How to execute multiple CLI commands in a python script?

From Dev

Unable to execute external python script from django

From Dev

How to write a python script that can get and execute python commands?

From Dev

Can I use a shell or python script to execute commands in a chroot?

From Dev

How to execute code in the Django shell by an external python script?

From Dev

How to execute external Python script with a program coded in Haxe?

From Dev

Execute gcloud commands in a bash script

From Dev

Bash script to spawn and execute commands

From Dev

Execute shell commands in Python

From Dev

How can I execute a bunch of Python commands before a script is run when using the Python interpreter?

From Dev

Execute Bash commands Python way

From Dev

commands execution in python script

From Dev

Cannot execute shell commands in bash script

From Dev

How to execute git commands from bash script?

From Dev

Execute multiple sage commands from php script

From Dev

How to execute the vim commands through shell script

From Dev

Execute commands inside subshell from script

From Dev

bash script execute commands after ssh

From Dev

Bash script to read from a file and execute commands

From Dev

Bash script to read from a file and execute commands

From Dev

Unable to execute root commands inside an openvpn script

From Dev

How to execute the following commands in a bash script?

From Dev

Make a shell script execute commands in telnet or programs

From Dev

How to execute commands in gnuplot using shell script?

From Dev

how to execute compound commands in shell script?

From Dev

script doesn't execute commands without tty

From Dev

Bash script to execute multiple Unix commands

From Dev

How do I execute a python script from the command line using custom commands?

Related Related

  1. 1

    How to execute multiple CLI commands in a python script?

  2. 2

    How to execute multiple CLI commands in a python script?

  3. 3

    Unable to execute external python script from django

  4. 4

    How to write a python script that can get and execute python commands?

  5. 5

    Can I use a shell or python script to execute commands in a chroot?

  6. 6

    How to execute code in the Django shell by an external python script?

  7. 7

    How to execute external Python script with a program coded in Haxe?

  8. 8

    Execute gcloud commands in a bash script

  9. 9

    Bash script to spawn and execute commands

  10. 10

    Execute shell commands in Python

  11. 11

    How can I execute a bunch of Python commands before a script is run when using the Python interpreter?

  12. 12

    Execute Bash commands Python way

  13. 13

    commands execution in python script

  14. 14

    Cannot execute shell commands in bash script

  15. 15

    How to execute git commands from bash script?

  16. 16

    Execute multiple sage commands from php script

  17. 17

    How to execute the vim commands through shell script

  18. 18

    Execute commands inside subshell from script

  19. 19

    bash script execute commands after ssh

  20. 20

    Bash script to read from a file and execute commands

  21. 21

    Bash script to read from a file and execute commands

  22. 22

    Unable to execute root commands inside an openvpn script

  23. 23

    How to execute the following commands in a bash script?

  24. 24

    Make a shell script execute commands in telnet or programs

  25. 25

    How to execute commands in gnuplot using shell script?

  26. 26

    how to execute compound commands in shell script?

  27. 27

    script doesn't execute commands without tty

  28. 28

    Bash script to execute multiple Unix commands

  29. 29

    How do I execute a python script from the command line using custom commands?

HotTag

Archive