2017-12-06 12 views
1

私は、仮想マシンを開始し、スパイスtcpポートを見つけ、リモートビューアを起動して仮想マシンとやりとりするという繰り返しタスクを自動化する簡単なスクリプトを作成しています。シーケンスのコマンドは次のとおりです。スパイス::5903://127.0.0.1Pythonはkvm-qemu/remote-viewerでユーザタスクを自動化します

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

これは、のようなものを出力します。 3番目のコマンドは、次のとおりです。

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

は私の命令の最初の二つは期待どおりに動作 - それはリモート - と3番目のコマンドです:

は、RHEL 7上のPython 2.7.5で
remote-viewer spice://127.0.0.1:5903 

私は、スクリプトを持っていますビューアーはエラーをスローします。

私はいくつかの異なることを試してみましたが、これは問題を引き起こす特殊文字だと思っていましたが、最初の2つのコマンドがうまく動作していると私は確信していません。私もこのような最初の2つのコマンドと同じ形式試してみました:

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を使用したくない場合は、Windows上でコマンドが起動されている下のリンクを見て、Shell = Trueを指定しないでUbuntuを実行してください。https://github.com/pankajigec26/Multiplatfrom_server_launcher/blob/master/handling_process.py –

関連する問題