2016-09-18 60 views
0

這樣的設計讓我哭,下面的代碼,請大家幫忙語法錯誤而使用子

def runbatch(CMD,HOST): 
    print CMD 
    print HOST 
    for host in HOST: 
     env.host_string=host 
     print CMD 
     print env.host_string 
     print "Execute command : \"%s\" at Host : %s" %(CMD,host) 
     print "-------------------------------------------------" 
     p=subprocess.Popen("run('ls')",shell=True, 
      stderr=subprocess.PIPE, 
      stdin=subprocess.PIPE) 
     output = p.communicate() 
     print output 

錯誤顯示

(無,「/ bin/sh的:-c:0行:靠近意外的標記'ls''\n/bin/sh: -c: line 0:運行語法錯誤( 'LS')「\ n」)

+0

你試圖從你的python腳本運行'ls'命令嗎?如果是這樣,請嘗試'p = subprocess.Popen(「ls」,shell = True)'。這應該列出當前標準輸出中的所有文件。 – TuanDT

+0

ls返回正常,但我需要使用fabric運行命令列出遠程主機 – Flasking

回答

0

subprocess.Popen()本地機器上運行bash命令。 fabric必須提供的是一種在本地機器上輸入命令的方法,其被髮送到並在遠程機器上運行。爲此,您需要一個fabfile.py(現在,您需要命名爲fabfile.py),其中存儲結構fabric.api.run()命令,該命令實際上是一個Python命令而不是bash命令。 fabric.api.run()的參數是在遠程機器上運行的bash命令。例如。的fabfile.py

from fabric.api import run 
from fabric.api import env 
def runcommand(): 
    run(env.my_command) 

使用這個例子中,你可以激活使用命令行fab --set my_command=some_bash_command -H remote_host_ip runcommand這個遠程調用。該字符串是您應在腳本中傳遞給subprocess.Popen()的字符串。例如。讓我們把你的腳本stackoverflow.py,在命令行參數使用bash的功能,可以在遠程機器

import subprocess 
import sys 

p=subprocess.Popen("fab --set my_command="+sys.argv[1]+" -H localhost runcommand",shell=True, 
        stderr=subprocess.PIPE, 
        stdin=subprocess.PIPE) 
output = p.communicate() 
print output 

採樣運行上執行:

Chip [email protected] 12:10:[email protected] ~: python stackoverflow.py ls 
[localhost] Executing task 'runcommand' 
[localhost] run: ls 
[localhost] out: AllArms.py    fines 
[localhost] out: Applications    github 
[localhost] out: Box Sync    grades_assgn1 
[localhost] out: DFExperiment    heuristic.py 
[localhost] out: Desktop     honour-project-in-thompson-sampling 
[localhost] out: Documents    jags_bin 
[localhost] out: Downloads    latemath 
[localhost] out: Dropbox     launchall.sh 
[localhost] out: FIT3080     launcher 
[localhost] out: GaussianExperiments   launchucb.sh 
[localhost] out: GoogleDrive    minuteSep5 
[localhost] out: HierarchicalStan.py   minutes22aug 
[localhost] out: IMG_6169.JPG    model1.pkl 
[localhost] out: Library     mydata 
[localhost] out: Monarch     notes15Aug2016 
[localhost] out: Movies     notesSep12 
[localhost] out: Music     old-honour 
[localhost] out: PTSTuneBeta    oracle.R 
[localhost] out: Pictures    paper 
[localhost] out: Public     parallelExperiments 
[localhost] out: Samsung     people_to_mark_first 
[localhost] out: WindowFrame.class   rezaPhone 
[localhost] out: WindowFrame.java   spike.py 
[localhost] out: a.out     stackoverflow.class 
[localhost] out: aaai.tar.gz    stackoverflow.cpp 
[localhost] out: all_experiments    stackoverflow.java 
[localhost] out: api4.csv    stackoverflow.py 
[localhost] out: atlas     test 
[localhost] out: boostlib    test.py 
[localhost] out: codes_and_data.tar.gz   test.txt 
[localhost] out: eclipse     test1.html 
[localhost] out: emo     test2.html 
[localhost] out: experimentlist    testlib.py 
[localhost] out: fabfile.py    testlib.pyc 
[localhost] out: fabfile.pyc    uselib.py 
[localhost] out: file1     uselib.pyc 
[localhost] out: file2 
[localhost] out: 


Done. 
Disconnecting from localhost... done. 
(None, "[localhost] Login password for 'hiennguyen': \n") 

重要提示:當調用fab這個方式,你可能必須:

  1. 啓用ssh訪問您的遠程機器。在這種情況下,遠程機器只是localhost

  2. 有時,遠程主機需要您輸入密碼,你不會被提示輸入密碼(這是我的機器上的情況下)。如果您等待一段時間並且什麼也沒看到,您可能需要輸入密碼,然後按ENTER鍵。

+0

明白了,謝謝! – Flasking