How to capture bash command output using python script - python

How to capture bash command output using python script.
For eg:
running below in linux :
[root#xxxxxx oracle]# echo sumit
sumit
[root#xxxxxx oracle]#
How can i re print only the above output using python script ? like running python test.py shoud give 'sumit' as output. i tried below:
test.py :
import sys
sys.stdout.flush()
out = sys.stdin.readline()
print(out)
Above prints only the input i type but not the already displayed output

With subprocess, you can run commands and check their return code, stdout and stderr outputs. Would that help?
For example:
import subprocess as proc
byte_output = proc.check_output(["ls", "-1", "."])
str_output = str(byte_output, "utf-8")
print(str_output)
# prints my local folders dev\ngit

Related

Execute multiline bash command from Python and get its output

I am under Python 3.8.10 in Ubuntu 20.04 trying to execute a multiline bash command and get its output. For this I am trying to combine this and this. My bash command is this:
/home/foo/.drsosc/drs-5.0.6/drscl << ASD
info
exit
ASD
and it works as I want. Now in Python I have this:
from pathlib import Path
import subprocess
PATH_TO_drscl = Path.home()/Path('.drsosc/drs-5.0.6/drscl')
def send_command(cmd: str):
execute_this = f'''{PATH_TO_drscl} << ASD
{cmd}
exit
ASD'''
return subprocess.run([execute_this], stdout=subprocess.PIPE)
print(send_command('info'))
but I get
FileNotFoundError: [Errno 2] No such file or directory: '/home/foo/.drsosc/drs-5.0.6/drscl << ASD\ninfo\nexit\nASD'
It seems that the problem is with the '\n' not being properly interpreted?
I found that this works as I want:
result = subprocess.run(
str(PATH_TO_drscl),
input = f'{cmd}\nexit',
text = True,
stdout = subprocess.PIPE
)
No, the problem is that you're trying to run small shell script but
you're calling an executable that has a name composed of all commands
in the script. Try with shell=True:
return subprocess.run([execute_this], stdout=subprocess.PIPE, shell=True)

Running a C executable inside a python program

I have written a C code where I have converted one file format to another file format. To run my C code, I have taken one command line argument : filestem.
I executed that code using : ./executable_file filestem > outputfile
Where I have got my desired output inside outputfile
Now I want to take that executable and run within a python code.
I am trying like :
import subprocess
import sys
filestem = sys.argv[1];
subprocess.run(['/home/dev/executable_file', filestem , 'outputfile'])
But it is unable to create the outputfile. I think some thing should be added to solve the > issue. But unable to figure out. Please help.
subprocess.run has optional stdout argument, you might give it file handle, so in your case something like
import subprocess
import sys
filestem = sys.argv[1]
with open('outputfile','wb') as f:
subprocess.run(['/home/dev/executable_file', filestem],stdout=f)
should work. I do not have ability to test it so please run it and write if it does work as intended
You have several options:
NOTE - Tested in CentOS 7, using Python 2.7
1. Try pexpect:
"""Usage: executable_file argument ("ex. stack.py -lh")"""
import pexpect
filestem = sys.argv[1]
# Using ls -lh >> outputfile as an example
cmd = "ls {0} >> outputfile".format(filestem)
command_output, exitstatus = pexpect.run("/usr/bin/bash -c '{0}'".format(cmd), withexitstatus=True)
if exitstatus == 0:
print(command_output)
else:
print("Houston, we've had a problem.")
2. Run subprocess with shell=true (Not recommended):
"""Usage: executable_file argument ("ex. stack.py -lh")"""
import sys
import subprocess
filestem = sys.argv[1]
# Using ls -lh >> outputfile as an example
cmd = "ls {0} >> outputfile".format(filestem)
result = subprocess.check_output(shlex.split(cmd), shell=True) # or subprocess.call(cmd, shell=True)
print(result)
It works, but python.org frowns upon this, due to the chance of a shell injection: see "Security Considerations" in the subprocess documentation.
3. If you must use subprocess, run each command separately and take the SDTOUT of the previous command and pipe it into the STDIN of the next command:
p = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE)
stdout_data, stderr_data = p.communicate()
p = subprocess.Popen(cmd, stdin=stdout_data, stdout=PIPE)
etc...
Good luck with your code!

How to access the output of a program via pipe in python

I'm trying to pipe the output of a python script using os.popen() . Here my python script :
sample.py
while(True):
print("hello")
python version : 3.6.7
os : ubuntu 18.04
My script to do the process :
import os
import types
def sample_function():
pipe = os.popen('python3 /home/gomathi/sample.py')
while(True):
a = pipe.readline()
yield a
s=sample_function()
for i in s:
print(i)
It works well for the above code.Now the problem is , i have changed the sample.py as follows :
sample.py
print("hello")
It just print blank for the entire screen and continues printing blank characters . What went wrong with my code ? What changes to be made in my code to work for the above sample.py ?
Your new sample.py ends, but you keep reading from the pipe. So you're getting empty strings.
Use the subprocess module, with the same function Popen and redirect the output and the error to the print function of your main .py file.
from subprocess import Popen, PIPE
command = ['python3', '/home/gomathi/sample.py']
process = Popen(command, shell=False, stdout=PIPE, stdin=PIPE)
while True:
line = process.stdout.readline() #variable_name_changed
print(line)

Subprocess wont take the output of dxl script

Im using Python's subprocess module to run a dxl script. My Problem is when i try to catch the Output (In this example a print-statement or a error message) of my dxl script, it is shown in the command prompt, but when i try to catch it with stdout=subprocess.PIPE or subprocess.check_output it always returns an empty string. Is there a way to catch the output or how could I get the Error messages from Doors?
It's important that you dont see the GUI of DOORS.
Here is my quick example that shows my problem:
test.dxl
print "Hello World"
test.py
import subprocess
doorsPath = "C:\\Program Files (x86)\\IBM\\Rational\\DOORS\\9.5\\bin\\doors.exe"
userInfo = ' -user dude -password 1234 -d 127.0.0.1 -batch ".\\test.dxl"'
dxl = " -W"
output = subprocess.check_output(doorsPath+dxl+userInfo)
print(output)
Edit: Using Windows 7 , DOORS 9.5 and Python 2.7
I know this post is pretty old, but the solution to the problem is to use
cout << ... instead of print. You can override the print perms like shown here
DOORS Print Redirect Tutorial for print, cout and logfiles
I'm feeling lucky here,
change print "Hello World" to cout << "Hello World"
and userInfo = ' -user dude -password 1234 -d 127.0.0.1 -batch ".\\test.dxl > D:\output.txt"', as in cmd promt the text can be directly exported to a text file.
your script have many error try this link for example for subprocess
and try this :
import subprocess
import sys
path = "C:\\Program Files(x86)\\IBM\\Rational\\DOORS\\9.5\\bin\\doors.exe"
userInfo = "C:\\Program Files (x86)\\IBM\\Rational\\DOORS\\9.5\\bin\\doors.exe"
proc = subprocess.Popen([path,userInfo,"-W"])
proc.communicate()
i hape it work on your system!

getting subprocess result in variable

I am executing python script using subprocess.call() in pytho script. The script which gets executed using subproecss is a server process which send result back to calling client.
result = subprocess.call('python -m module/coref_resolution/src/coref/corenlp &', shell = True)
Is there any way to receive result from corenlp.py into result variable?
import shlex
cmd = shlex.split('your command')
output = subprocess.Popen( cmd, stdout = subprocess.PIPE).communicate()[0]

Categories

Resources