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")
subprocess.Popen()
runs a bash command on your local machine . 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 . 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. The argument of fabric.api.run()
is a bash command that runs on the remote machine . Eg of a 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
. This string is the string you should pass to subprocess.Popen()
in your script. 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
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:
Enable ssh
access to your remote machine . In this case, the remote machine is just localhost
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.