Reputation: 131
I'm working on a simple script to automate a repetitive task of starting a virtual machine, finding it's spice tcp port and starting remote-viewer to interact with the virtual machine. The commands in sequence are:
virsh -c qemu:///system start test_machine
virsh -c qemu:///system domdisplay test_machine
This outputs something like: spice://127.0.0.1:5903. The third command is:
remote-viewer spice://127.0.0.1:5903
In python 2.7.5 on RHEL 7 I have the script:
#!/usr/bin/python
import subprocess
import pipes
machine_name = 'test_machine'
return_code = subprocess.check_output(['virsh', '-c', 'qemu:///system', 'start', machine_name])
return_code = subprocess.check_output(['virsh', '-c', 'qemu:///system', 'domdisplay', machine_name])
print str(return_code)
esc_return_code = pipes.quote(return_code)
print "remote-viewer {}".format(return_code)
#proc = subprocess.check_output(["remote-viewer {}".format(esc_return_code)]) #, return_code])
The first two of my commands work as expected--it's the third command with remote-viewer that throws an error.
I've tried a few different things thinking it was special characters causing a problem but now that I see the first two commands working just fine I'm not convinced. I also tried the same format as the first two commands like this:
subprocess.check_output(['remote-viewer', return_code])
Running the script as is gives the output:
spice://127.0.0.1:5903
remote-viewer spice://127.0.0.1:5903
Traceback (most recent call last):
File "/mnt/data/Scripts/runvm.py", line 13, in <module>
proc = subprocess.check_output(["remote-viewer {}".format(esc_return_code)]) #, return_code])
File "/usr/lib64/python2.7/subprocess.py", line 568, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
If I hard code the values (no variables) it works. Like this:
subprocess.check_output(['remote-viewer spice://127.0.0.1:5903'])
What am I missing?
Upvotes: 2
Views: 865
Reputation: 2615
You can use Popen and shell=True to run the command
From subprocess import Popen
output='spice://127.0.0.1:5903'
Command='remote-viewer'+' '+output
#Proc=Popen(Command,shell=True)
Proc=Popen(Command)
Upvotes: 1