2017-12-06 3 views
1

가상 머신을 시작하고 가상 머신과 상호 작용하기 위해 원격 뷰어를 시작하는 반복적 인 작업을 자동화하는 간단한 스크립트 작업을하고 있습니다. . 시퀀스의 명령은 다음과 같습니다.Python은 kvm-qemu/remote-viewer에서 사용자 작업을 자동화합니다

virsh -c qemu:///system start test_machine 
virsh -c qemu:///system domdisplay test_machine 

이렇게하면 spice : //127.0.0.1 : 5903과 같은 결과가 출력됩니다. 세 번째 명령은 다음과 같습니다

#!/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]) 

내 명령의 처음 두가 예상대로 작동 - 그것은 remote-와 세 번째 명령입니다 :

는 RHEL 7 파이썬 2.7.5에서
remote-viewer spice://127.0.0.1:5903 

나는 스크립트가 뷰어에서 오류가 발생했습니다.

나는 특수 문자가 문제를 일으키는 것이라고 생각한 몇 가지 다른 것들을 시도했지만, 이제는 처음 두 명령이 잘 작동한다는 것을 알았지 만 나는 확신하지 못한다. 나는 또한이 같은 처음 두 명령과 같은 형식 시도 :

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 

내가 하드 코드 값 (변수없이)가 작동하는 경우 :

subprocess.check_output(['remote-viewer', return_code]) 

같이 스크립트를 실행을 출력을 제공합니다. 좋아요 :

subprocess.check_output(['remote-viewer spice://127.0.0.1:5903']) 

무엇이 누락 되었습니까?

답변

1

당신은 명령이 확실히 작동

From subprocess import Popen 
output='spice://127.0.0.1:5903' 
Command='remote-viewer'+' '+output 
#Proc=Popen(Command,shell=True) 
Proc=Popen(Command) 
+0

을 실행하는 진정한는 popen과 쉘 = 사용할 수 있습니다. 그것을하는 더 안전한 방법 있는가? 쉘이 이런 식으로 오용 될 수 있음을 이해합니다. 나는 최소한 pipe.quote()로 명령의 이스케이프 된 버전을 사용하고 싶습니다만, "No such file or directory"로 되돌아갑니다. – FirmwareRootkits

+0

shell = True를 사용하고 싶지 않다면 Shell = True가 아닌 Windows 및 Ubuntu에서 명령이 실행 된 아래 링크를 살펴보십시오 https://github.com/pankajigec26/Multiplatfrom_server_launcher/blob/master/handling_process.py –

관련 문제