使用子进程

时间:2016-09-18 03:54:31

标签: python subprocess fabric

这个设计让我哭泣,下面的代码,请帮助

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”)

1 个答案:

答案 0 :(得分: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 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")

重要提示:以这种方式致电fab时,您可能需要:

  1. 启用ssh访问远程计算机。在这种情况下,远程计算机只是localhost

  2. 有时,远程主机要求您输入密码, >将提示您输入密码(我的机器就是这种情况)。如果您等待一段时间并且什么也看不见,您可能需要输入密码然后按ENTER键。