def run_parallel(self, hosts, cmd):
codes = {"total": 0, "error": 0, "success": 0}
def worker(host, cmd):
p = Popen(self.get_parallel_ssh_options(host, cmd), stdout=PIPE, stderr=PIPE)
while True:
outs, _, _ = select([p.stdout, p.stderr], [], [])
if p.stdout in outs:
outline = p.stdout.readline()
else:
outline = ""
if p.stderr in outs:
errline = p.stderr.readline()
else:
errline = ""
if outline == "" and errline == "" and p.poll() is not None:
break
if outline != "":
print("%s: %s" % (colored(host, "blue", attrs=["bold"]), outline.strip()))
if errline != "":
print("%s: %s" % (colored(host, "blue", attrs=["bold"]), colored(errline.strip(), "red")))
if p.poll() == 0:
codes["success"] += 1
else:
codes["error"] += 1
codes["total"] += 1
pool = Pool(self.ssh_threads)
for host in hosts:
pool.start(Greenlet(worker, host, cmd))
pool.join()
self.print_exec_results(codes)
评论列表
文章目录