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!
Related
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
i am developing a coffeescript which needs to execute a python program and get the responses from the python code.
my coffee script is
module.exports = (robot) ->
robot.respond /greetme/i, (msg) ->
sender = msg.message.user.name.toLowerCase()
#exec = require('child_process').exec
command = "python3 ext-scripts/hello.py"
#exec command, (out1) ->
msg.send out1
msg.send "Hello " + sender
msg.finish()
and my python code is hello.py
print("hey indhu")
return "reached the python file"
i need to get the output "reached the python file" to 6th line in coffee script. to send out the message
i am getting error while doing this exec out function.
message: Error: Command failed: python3 ext-scripts/hello.py File "ext-scripts/hello.py", line 28
return "reached the python file"
^ SyntaxError: 'return' outside function
error: Response not OK: no_text
How to make it work. please help me . i am a python developer and new to coffeescript.
The easiest way is with a supprecess that reads the output and filters. The first version is good for Windows machines, the second for Linux systems.
import subprocess
process = subprocess.Popen(['echo', 'More output'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
stdout, stderr
OR
import os
cmd = 'your command here'
terminal_output = os.system(cmd)
print(terminal_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)
I've been working on this for a long time, and any help would be appreciated.
What I am trying to do here is ssh to a testing server, then cd .., and then print a list of the directories in that folder through python. This code is my best attempt:
def subprocess_cmd(command):
process = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
print "Test 1"
proc_stdout = process.communicate()[0].strip()
#proc_stdout= process.stdout.readlines() (Gives same outcome as communicate)
#proc_stdout= process.stdout.read() (Gives same outcome as communicate)
print "Test 2"
print proc_stdout
subprocess_cmd('ssh user#server -p 111;cd ..;ls')
For some reason this function always hangs at the "proc_stdout= "step. It never prints "Test 2" or returns a list of files. It works fine if I take out the ssh command though. What I expect to see in the terminal is something like this, but instead the terminal hangs, and I can't interact with it anymore:
dredbounds-computer: python file_name.py
Test 1
Test 2
FileA
FileB
FileC
Update:
I modified the code and and put proc_stdout= process.stderr. communicate().
Here is my updated code:
def subprocess_cmd(command):
process = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
print "Test 1"
proc_stderr= process.stderr. communicate()
print "Test 2"
print proc_stderr
print "Test 3"
Running this I am getting the following error in the terminal:
dredbounds-computer: python terminal_test.py
Test 1
Traceback (most recent call last):
File "file_name.py", line 26, in <module>
subprocess_cmd('ssh user#server -p 111;cd ..;ls')
File "terminal_test.py", line 21, in subprocess_cmd
proc_stdout= process.stderr. communicate()
AttributeError: 'NoneType' object has no attribute 'communicate'
Does anyone know how I can fix this code, or another way of doing the same thing. Not sure why this is giving me a none type error. Is there something wrong with how I call my ssh command? I've entered the same commands manually in the terminal and it returns a list of directories, so it should work in theory. Any advice?
If you want just to list directory contents, you can send some command over SSH.
Bash:
ssh 192.168.122.24 ls /tmp
or if you want to use "cd" as in your question:
ssh 192.168.122.24 "cd /tmp; ls"
Python script example:
import subprocess
HOST = 'server'
PORT = '111'
USER = 'user'
CMD = 'cd /tmp; ls'
process = subprocess.Popen(['ssh', '{}#{}'.format(USER, HOST),
'-p', PORT, CMD],
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
result = process.stdout.readlines()
if not result:
err = process.stderr.readlines()
print('ERROR: {}'.format(err))
else:
print(result)
I need to build a server that can execute a command line command and send back the output of the command
Example:
for the command- echo hello world, the server will return the string "hello world".
I tried to use subprocess.call() function but it returns a number and not a string. I have the sever ready, i just need this this.
Code:
type=struct.pack("B",2) #packing type
data=subprocess.call(client_data, shell=True)
length=struct.pack("H",len(data)) #packing lenght
client_soc.send(type+length+data)
How about using subprocess.check_output instead? From the manual:
"Run command with arguments and return its output as a byte string."
It would be something like data = subprocess.check_output(client_data, shell=True) then.
See this man page for more information.
Maybe this piece of code will help
import subprocess
proc = subprocess.Popen('ping google.com', stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
tmp = proc.stdout.read()
print tmp