简体   繁体   English

使用子流程时,意外令牌附近的语法错误

[英]syntax error near unexpected token while use subprocess

This design makes me cry,code below,please help 此设计使我哭泣,代码如下,请帮助

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

error shows 错误显示

(None, "/bin/sh: -c: line 0: syntax error near unexpected token 'ls''\\n/bin/sh: -c: line 0: run('ls')'\\n") (无,“ / bin / sh:-c:第0行:意外令牌'ls''\\n/bin/sh: -c: line 0:附近的语法错误'ls''\\n/bin/sh: -c: line 0: run('ls')'\\ n“)

subprocess.Popen() runs a bash command on your local machine . subprocess.Popen()本地计算机上运行bash命令。 What fabric has to offer is a way to enter a command on local machine which got sent to and run on a remote machine . fabric所提供的是一种在本地计算机上输入命令的方法,该命令已发送到远程计算机并在远程计算机上运行。 To this end, you need a fabfile.py (for now, you need to name it precisely fabfile.py ) where you store the fabric fabric.api.run() command, which actually is a Python command and not a bash command. 为此,您需要一个fabfile.py (现在,您需要精确地命名为fabfile.py ),在其中存储fabric.api.run()命令,该命令实际上是Python命令而不是bash命令。 The argument of fabric.api.run() is a bash command that runs on the remote machine . fabric.api.run()的参数是在远程计算机上运行的bash命令。 Eg of a fabfile.py 例如fabfile.py

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

Using this example, you could activate this remote call by using command line fab --set my_command=some_bash_command -H remote_host_ip runcommand . 使用此示例,可以使用命令行fab --set my_command=some_bash_command -H remote_host_ip runcommand激活此远程调用。 This string is the string you should pass to subprocess.Popen() in your script. 该字符串是您应该传递给脚本中的subprocess.Popen()的字符串。 Eg let's call your script stackoverflow.py that takes in a command line argument the bash function to be executed on the remote machine 例如,让我们调用您的脚本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

Sample run: 样品运行:

Chip chip@ 12:10:58@ ~: 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")

IMPORTANT NOTE : When calling fab this way, you might have to: 重要说明 :以这种方式调用fab ,您可能必须:

  1. Enable ssh access to your remote machine . 启用对远程计算机的 ssh访问。 In this case, the remote machine is just localhost 在这种情况下, 远程机器就是localhost

  2. Sometimes, the remote host requires you to enter password and you will not be prompted to enter password (this is the case on my machine). 有时, 远程主机要求您输入密码,而不会提示您输入密码(在我的机器上就是这种情况)。 If you wait for awhile and see nothing, you might want to enter the password then hit ENTER. 如果您稍等片刻却看不到任何内容,则可能要输入密码,然后按Enter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM