Fetching the output of a command executed through os.system() command [duplicate] - python

This question already has an answer here:
How to get the output from os.system()? [duplicate]
(1 answer)
Closed 1 year ago.
I am using a script where I issue a shell command to a remote server (ssh) using os.system() command. I need to collect the output of the command I executed on the remote server. The problem is the double redirection. I am using the os.system() to execute a ssh command which executes the intended command on a remote server. It is this output I intend to make use of. I just need some pointers as to how this can be achieved ?

Use the subprocess module:
subprocess.check_output returns the output of a command as a string.
>>> import subprocess
>>> print subprocess.check_output.__doc__
Run command with arguments and return its output as a byte string.
If the exit code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and output in the output attribute.

Related

Call external command from python without using neither os.system nor subprocess

Is there any other way to call external command from python without using neither os.system nor subprocess.
This is related to my previous question ununderstandable behavior subprocess.Popen(cmd,stdout) and os.system(cmd) The problem is that os.system and subprocess doesn't return all expected output.

Running a Python print statement containing a bash command [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 5 years ago.
Is there a way for the Python print statement containing a bash command to run in the terminal directly from the Python script?
In the example below, the awk command is printed on the terminal.
#!/usr/bin/python
import sys
print "awk 'END{print NF}' file"
I can of course think of writing the print statement in a separate file and run that file as a bash script but is there a way to run the awk command directly from the python script rather than just printing it?
is there a way to run the awk command directly from the python script rather than just printing it?
Yes, you can use subprocess module.
import subprocess
subprocess.call(["ls", "-l"])
You can pipe your Python output into a Bash process, for example,
python -c "print 'echo 5'" | bash
will output
5
You could even use the subprocess module to do that from inside Python, if you wanted to.
But I am sure this is pretty bad design, and not a good idea. If you get your coding wrong, there's a risk you could allow hostile users to execute arbitrary commands on the machine running your code.
One solution is to use subprocess to run a shell command and capture its output, for example:
import subprocess
command = "awk 'END{print NF}' file"
p = subprocess.Popen([command], shell=True, bufsize=2000,
stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
(child_stdout, child_stdin) = (p.stdout, p.stdin)
print(''.join([line for line in child_stdout]))
child_stdout.close()
p.stdout.close()
Adjust bufsize accordingly based on the size of your file.

How to run bash commands inside of a Python script [duplicate]

This question already has answers here:
Running Bash commands in Python
(11 answers)
Closed 2 years ago.
I am trying to run both Python and bash commands in a bash script.
In the bash script, I want to execute some bash commands enclosed by a Python loop:
#!/bin/bash
python << END
for i in range(1000):
#execute‬ some bash command such as echoing i
END
How can I do this?
Use subprocess, e.g.:
import subprocess
# ...
subprocess.call(["echo", i])
There is another function like subprocess.call: subprocess.check_call. It is exactly like call, just that it throws an exception if the command executed returned with a non-zero exit code. This is often feasible behaviour in scripts and utilities.
subprocess.check_output behaves the same as check_call, but returns the standard output of the program.
If you do not need shell features (such as variable expansion, wildcards, ...), never use shell=True (shell=False is the default). If you use shell=True then shell escaping is your job with these functions and they're a security hole if passed unvalidated user input.
The same is true of os.system() -- it is a frequent source of security issues. Don't use it.
Look in to the subprocess module. There is the Popen method and some wrapper functions like call.
If you need to check the output (retrieve the result string):
output = subprocess.check_output(args ....)
If you want to wait for execution to end before proceeding:
exitcode = subprocess.call(args ....)
If you need more functionality like setting environment variables, use the underlying Popen constructor:
subprocess.Popen(args ...)
Remember subprocess is the higher level module. It should replace legacy functions from OS module.
I used this when running from my IDE (PyCharm).
import subprocess
subprocess.check_call('mybashcommand', shell=True)

Running shell commands within python interpreter [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 8 years ago.
Is there a simple method for calling shell command line arguments (like ls or pwd) from within python interpreter?
In plain python, you need to use something along the lines of this:
from subprocess import check_output
check_output("ls", shell=True)
In IPython, you can run either of those commands or a general shell command by starting off with !. For example
! echo "Hello, world!" > /tmp/Hello.txt
If you're using python interactively, you would almost certainly be happier with IPython.
If you meant to use the Python shell interactively while being able to call commands (ls, pwd, ...) check out iPython.

passing commands to application CLI from python script [duplicate]

This question already has answers here:
How to give input streams in python?
(2 answers)
Closed 9 years ago.
To run shell command from python script, I generally use subprocess or os.system module.
Using that I am running some shell command from python script which is initiating another application and that application also has command line interface.
How would I pass commands to that application CLI from my python script?
How can I capture the output of application CLI from my python script?
It is highly appreciated if someone can suggest material or example code.
The application you're initiating might behave differently when running through a subprocess. Specifically, when connected to a process pipe, some applications buffer their output by default instead of flushing line by line. If the application you're running flushes its output, you can get it realtime, otherwise, you'll only get output when the buffer is full.
That said, here's an example to run some application:
p = subprocess.Popen(['someapp', 'param1', 'param2'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE,)
# sends the command "some_command" to the app:
p.stdin.write('some_command\n')
# waits for a single line from the output
result = p.stdout.readline()
If it hangs on p.stdout.readline() that means the output is being buffered.

Categories

Resources