syntax error near unexpected token while use subprocess
<p>This design makes me cry,code below,please help</p>

<pre><code>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
</code></pre>

<p>error shows</p>

<p><em>(None, "/bin/sh: -c: line 0: syntax error near unexpected token <code>'ls''\n/bin/sh: -c: line 0:</code>run('ls')'\n")</em></p>

<p><code>subprocess.Popen()</code> runs a bash command on your <strong>local machine</strong>. What <code>fabric</code> has to offer is a way to enter a command on <em>local machine</em> which got sent to and run on a <em>remote machine</em>. To this end, you need a <code>fabfile.py</code> (for now, you need to name it precisely <code>fabfile.py</code>) where you store the fabric <code>fabric.api.run()</code> command, which actually is a Python command and not a bash command. The argument of <code>fabric.api.run()</code> is a bash command that runs on the <em>remote machine</em>. E.g. of a <code>fabfile.py</code></p>

<pre><code>from fabric.api import run
from fabric.api import env
def runcommand():
    run(env.my_command)
</code></pre>

<p>Using this example, you could activate this remote call by using command line <code>fab --set my_command=some_bash_command -H remote_host_ip runcommand</code>. This string is the string you should pass to <code>subprocess.Popen()</code> in your script. E.g. let's call your script <code>stackoverflow.py</code> that takes in a command line argument the bash function to be executed on the <em>remote machine</em></p>

<pre><code>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
</code></pre>

<p>Sample run:</p>

<pre><code>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")
</code></pre>

<p><strong>IMPORTANT NOTE</strong>: When calling <code>fab</code> this way, you might have to:</p>

<ol>
<li><p>Enable <code>ssh</code> access to your <em>remote machine</em>. In this case, the <em>remote machine</em> is just <code>localhost</code></p></li>
<li><p>Sometimes, the <em>remote host</em> requires you to enter password and you <strong>will not</strong> 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. </p></li>
</ol>

python subprocess fabric