本文共 1311 字,大约阅读时间需要 4 分钟。
python多线程批量远程管理程序,是老男孩linux培训 python课程教学案例内容,有关该内容的视频教学,请继续关注老男孩的博客。
#!/usr/bin/env python# this script comes from beijing oldboy trainning.# e_mail:70271111@qq.com# function: remote multi exec cmd by ssh.# version:1.1################################################# oldboy trainning info. # QQ 80042789 70271111# site:http://www.etiantian.org# blog:http://oldboy.blog.51cto.com# oldboy trainning QQ group: 208160987 45039636################################################import multiprocessingimport os,sysimport timeresult = []f = file('client.txt')cmd = sys.argv[1]excution_list = []for line in f.readlines(): f_line = line.strip().split() host = f_line[0] username = f_line[1] port = f_line[2] if f_line[3] == 'PASSWORD': password = f_line[4] call_ssh = 'python batch_ssh.py %s %s %s PASSWORD %s %s' %(host,username,port,password,cmd) print call_ssh else: call_ssh = 'python batch_ssh.py %s %s %s SSH_KEY %s' %(host,username,port,cmd) excution_list.append(call_ssh)def run_cmd(run_task): os.system(run_task)#print excution_listp = multiprocessing.Pool(processes=len(excution_list))for task in excution_list: result.append(p.apply_async(run_cmd,(task,)))p.close()for r in result: r.get(timeout=10)
client.txt 是服务器的 ip 用户名 密码列表
batch_ssh.py 内容见
转载地址:http://ojyno.baihongyu.com/