确定。我有两个shell命令返回字符串结果,然后我需要进一步处理。第一个命令产生一个结果(主机名),然后下一个命令获取第一个结果,以扩展另一个更广泛的主机名列表。这是为了生成一个主机列表来运行另一个shell命令。
我不需要一个完整的例子,只需要朝着正确的方向转向。
import subprocess
def getMasterList():
p1 = subprocess.Popen(['get_master_list'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', '-v', '\['], stdin=p1.stdout, stdout=subprocess.PIPE)
master_list = p2.communicate()[0]
return master_list.split()
def getHostsFromList(master):
p1 = subprocess.Popen(['get_hosts_from_master', master], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', '-v', '\['], stdin=p1.stdout, stdout=subprocess.PIPE)
print p2.communicate()[0]
def doStuff(host):
p = subprocess.Popen('run_shell_command', shell=True, stdout=subprocess.PIPE)
print p.stdout.read()
for master in getMasterList():
getHostsFromList(master)
# How do I iterate further to run doStuff() over the hosts from getHostsFromList(master)?
答案 0 :(得分:0)
也许我错过了什么,但不仅仅是:
def getHostsFromList(master):
p1 = subprocess.Popen(['get_hosts_from_master', master], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['grep', '-v', '\['], stdin=p1.stdout, stdout=subprocess.PIPE)
return p2.communicate()[0]
for master in getMasterList():
for host in getHostsFromList(master):
doStuff(host)