getting output of command in subprocess - python

I saw a few threads for this, but everything there didn't help me.
I'm running a subprocess to run commands on cmd via python(using 2.7)
p = subprocess.Popen(["start", "cmd", "/k", command], shell=True)
This command works and everything, but I can't manage to capture the output of the command.
I tried check_output or specifying stdout=sp.PIPE or stdout=file, but it didn't work.
Any suggestion will be appreciated.
Thanks

check_output should work fine:
from subprocess import check_output
out = check_output(["echo", "Test"], shell=True)
Output of command:
>>> print out
Test

Related

Subprocess no output when running a shell

I tried to run a system shell in Python subprocess module:
p = subprocess.Popen("/bin/bash", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
print(p.communicate())
However, it printed (b'', b'')
I tried other shells too, but they all failed to work. (Zsh, Sh, and FiSH)
How can I get the output? Thanks in advance!
It works for me as you would expect I guess.
(base) tzane:~/python_test$ python test_1.py
whereis ls
exit
(b'ls: /bin/ls /usr/share/man/man1/ls.1.gz\n', b'')
(base) tzane:~/python_test$
You are not getting any output if you are not telling bash to output anything. Calling .wait() with pipes can cause deadlocks so I would do something like this instead
import subprocess
p = subprocess.Popen("/bin/bash", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
print(p.communicate(timeout=30))
except TimeoutExpired:
p.kill()
print(p.commnunicate())
as suggested in the documentation or just use subprocess.run if you don't have to interact with the subprocess and you just want to catch the output.

How to Run a Specific Powershell Command Within Python with a Defined Variable and Print Output?

I am trying to run a specific command from Python to Powershell:
The command works as expected in Powershell. The command in Powershell is as following:
gpt .\Method\gpt_scripts\s1_cal_deb.xml -t .\deburst\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E_Cal_deb_script.dim .\images\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E.zip
Powershell Output:
os.getcwd()
'C:\\Users\\Ishack\\Documents\\Neta-Analytics\\Harvest Dates\\S1_SLC_Processing'
The current directory is the same as in PowerShell
I tried something like this:
import subprocess
process = 'gpt .\Method\gpt_scripts\s1_cal_deb.xml -t .\deburst\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E_Cal_deb_script.dim .\images\S1A_IW_SLC__1SDV_20180909T003147_20180909T003214_023614_0292B2_753E.zip'
process = subprocess.Popen(['powershell.exe', '-NoProfile', '-Command', '"&{' + process + '}"'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process
Output:
<subprocess.Popen at 0x186bb202388>
But when I press enter I get no response, I would like Python to print out the output just like in Powershell. I researched other similar questions but still no solution to the problem.
Thanks,
Ishack
import os
os.system("powershell.exe -ExecutionPolicy Bypass -File .\SamplePowershell.ps1")
SamplePowershell.ps1
Write-Host "Hello world"
hostname
This worked for me. Commands in file SamplePowershell.ps1.
and if you want to use subprocess
import subprocess
k=subprocess.Popen('powershell.exe hostname', stdout=subprocess.PIPE, stderr=subprocess.PIPE);
print(k.communicate())

Python: get stdout from background subprocess

i'm trying to get informations of a network interface on a linux machine with a python script, i.e. 'ifconfig -a eht0'. So i'm using the following code:
import subprocess
proc = subprocess.Popen('ifconfig -a eth0', shell=True, stdout=subprocess.PIPE)
proc.wait()
output = proc.communicate()[0]
Well if I execute the script from terminal with
python myScript.py
or with
python myScript.py &
it works fine, but when it is run from background (launched by crontab) without an active shell, i cannot get the output.
Any idea ?
Thanks
Have you tried to used "screen"?
proc = subprocess.Popen('screen ifconfig -a eth0', shell=True, stdout=subprocess.PIPE)
I'm not sure that it can work or not.
Try proc.stdout.readline() instead of communicate, also stderr=subprocess.STDOUT in subprocess.Popen() might help. Please post the results.
I found a solution to the problem, i guess that the system is not able to recognize the function ifconfig when executed by the crontab. So adding the full path to the subprocess allows the script to be executed properly:
`proc = subprocess.Popen('/sbin/ifconfig -a eth0',shell=True,stdout=subprocess.PIPE)
proc.wait()
output = proc.communicate()[0]`
and now i can manage the output string.
Thanks

Invoking history command on terminal via Python script

I am running a python script which is using subprocess to execute "history" command on my Ubuntu terminal. Apparently,I am getting this error
history: not found
I got to know that history can not be invoked by any script by default.
What can I do to overcome this? Or any other possible alternatives.
readline.get_history_item() method isnt working either.
Use this:
from subprocess import Popen, PIPE, STDOUT
e = Popen("bash -i -c 'history -r;history' ", shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = e.communicate()

Running a bash script from Python

I need to run a bash script from Python. I got it to work as follows:
import os
os.system("xterm -hold -e scipt.sh")
That isn't exactly what I am doing but pretty much the idea. That works fine, a new terminal window opens and I hold it for debugging purposes, but my problem is I need the python script to keep running even if that isn't finished. Any way I can do this?
I recommend you use subprocess module: docs
And you can
import subprocess
cmd = "xterm -hold -e scipt.sh"
# no block, it start a sub process.
p = subprocess.Popen(cmd , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# and you can block util the cmd execute finish
p.wait()
# or stdout, stderr = p.communicate()
For more info, read the docs,:).
edited misspellings

Categories