2

行。我有两个返回字符串结果的 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)?
4

1 回答 1

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)
于 2013-07-26T23:09:14.920 回答