使用子进程时意外标记附近出现语法错误

syntax error near unexpected token while use subprocess

这个设计让我哭了,代码在下面,请帮助

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

错误显示

(None, "/bin/sh: -c: 第 0 行: 意外标记附近的语法错误 'ls''\n/bin/sh: -c: line 0:运行('ls')'\n")

subprocess.Popen() 运行在您的 本地计算机 上执行 bash 命令。 fabric 必须提供一种在 本地机器 上输入命令的方法,该命令被发送到 运行 在 远程机器。为此,你需要一个fabfile.py(现在,你需要准确命名它fabfile.py),用于存储fabric 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() 的字符串。例如。让我们调用你的脚本Whosebug.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 chip@ 12:10:58@ ~: python Whosebug.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                  Whosebug.class
[localhost] out: aaai.tar.gz                Whosebug.cpp
[localhost] out: all_experiments                Whosebug.java
[localhost] out: api4.csv               Whosebug.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。