This question already has answers here:
How to use `subprocess` command with pipes
(7 answers)
Closed 5 years ago.
I write this command on linux
netstat -ant | wc -l
but when I try to call from python with
subprocess.Popen(['netstat','-ant','|','wc','-l'])
I cant get all output, I see just result of first command (netstat -ant).
How can I process this command on python ? (note: this command gives a int as a result)
I don't know if there's any easier method but you can go like:
from subprocess import run, Popen, PIPE
sess1 = run(['netstat', 'ant'], stdout=PIPE)
sess2 = Popen(['grep', '"SYN"'], stdin=PIPE)
sess2.stdin.write(sess1.stdout)
sess2.communicate() # required?
Related
This question already has answers here:
live output from subprocess command
(21 answers)
Closed 2 years ago.
I'm writing a script to get netstat status using subprocess.check_output.
cmd = 'netstat -nlpt'
result = subprocess.check_output(cmd, shell=True, timeout=1800)
print(result.decode('utf-8'))
The above is running perfectly. Is there any way to get the live-streaming output. I have heard poll() function does this job. In live output from subprocess command they are using popen but i'm using check_output please some one help me on this issue. Thank you!
From this answer:
The difference between check_output and Popen is that, while popen is a non-blocking function (meaning you can continue the execution of the program without waiting the call to finish), check_output is blocking.
Meaning if you are using subprocess.check_output(), you cannot have a live output.
Try switching to Popen().
This question already has answers here:
How do I write to a Python subprocess' stdin?
(5 answers)
How do I pass a string into subprocess.Popen (using the stdin argument)?
(12 answers)
Write to a Python subprocess's stdin without communicate()'s blocking behavior
(1 answer)
How to use `subprocess` command with pipes
(7 answers)
Closed 3 years ago.
I am using Python and wants to run the "editUtility" as shown below.
echo "Some data" | /opt/editUtility --append="configuration" --user=userid 1483485
Where 1483485 is some random number and passed as parameter too.
What I am doing is calling the "editUtility" via Python "subprocess" and passing the param as shown below.
proc = subprocess.Popen(['/opt/editUtility', '--append=configuration'],stdout=subprocess.PIPE)
lsOutput=""
while True:
line = proc.stdout.readline()
lsOutput += line.decode()
if not line:
break
print(lsOutput)
My question is: How to pass all of the params mentioned above and how to fit the 'echo "some data"' along with pipe sign with subprocess invocation?
so if you just want to input a string and then read the output of the process until the end Popen.communicate can be used:
cmd = [
'/opt/editUtility',
'--append=configuration',
'--user=userid',
'1483485'
]
proc = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
(stdoutData, stderrData) = proc.communicate('Some data')
This question already has an answer here:
Opening a process with Popen and getting the PID
(1 answer)
Closed 3 years ago.
I want to use python to execute shell commands, how to get the process ID of the process generated by the shell command.
import subprocess
subprocess.Popen(['{}'.format(self.executor), '-c', {}'.format(config), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
The process executed by this shell command does not exit by itself, I need to run 10 min to KILL it, how can I get the id of the process?
You have to store the object Popen returns then use its pid attribute:
p = subprocess.Popen(...)
print(p.pid)
This question already has answers here:
Pipe subprocess standard output to a variable [duplicate]
(3 answers)
Python subprocess Popen stdout to variable only [duplicate]
(1 answer)
Redirect subprocess to a variable as a string [duplicate]
(2 answers)
Closed 4 years ago.
I'd like to retrieve the output from a shell command
In [7]: subprocess.Popen("yum list installed", shell=True)
Out[7]: <subprocess.Popen at 0x7f47bcbf6668>
In [8]: Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
GeoIP.x86_64 1.5.0-11.el7 #anaconda
NetworkManager.x86_64
....
The results are output to the console,
How could I hold the output to a variable saying "installed_tools"?
Try setting stdout and/or stderr to subprocess.PIPE.
import subprocess as sp
proc = sp.Popen("yum list installed", shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
out = proc.stdout.read().decode('utf-8')
print(out)
As suggested in comments, it's better to use Popen.communicate() in case stderr needs reading and gets blocked. (Thanks UtahJarhead)
import subprocess as sp
cp = sp.Popen("yum list installed", shell=True, stdout=sp.PIPE, stderr=sp.PIPE).communicate()
out = cp[0].decode('utf-8')
print(out)
This question already has answers here:
Read streaming input from subprocess.communicate()
(7 answers)
Closed 7 years ago.
I have a script that reads from external sensors (and runs forever), when I run it as ./zwmeter /dev/ttyUSB0 300 it behaves normally and prints output continuously to stdout. I am using bash on Ubuntu. I'd like to execute this command as part of a python script. I have tried:
from subprocess import Popen, PIPE
proc = Popen(['./zwmeter', '/dev/ttyUSB0', '300'], stderr=PIPE, stdout=PIPE)
print proc.communicate()
but I get a program that runs forever without producing any output. I don't care about stderr, only stdout and have tried splitting up the printing but still with no success.
Thank you for any help you can provide!
I think the problem has to do with the process I'm calling not terminating. I found a good work around on this site:
http://eyalarubas.com/python-subproc-nonblock.html