#_*_coding:utf8_*_
from multiprocessing import Process, Pool
import paramiko
import sys,os
host_list = (
('192.168.1.91', 'root','654321',123456),
('192.168.1.93', 'root', '654321',123456),
)
s = paramiko.SSHClient() #绑定实例
s.load_system_host_keys() #加载本机HOST主机文件
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def ssh_run(host_info):
ip,username,password,newpassword = host_info
s.connect(ip,22,username,password,timeout=5) #连接远程主机
stdin,stdout,stderr=s.exec_command('echo %s | passwd --stdin root'%(newpassword))
#stdin,stdout,stderr=s.exec_command('df')
cmd_result = stdout.read(),stderr.read() #读取命令结果
print '\033[32;1m-------------%s-----------\033[0m' % ip, username
for line in cmd_result:
print line,
p = Pool(processes=2)
result_list = []
for h in host_list:
result_list.append(p.apply_async(ssh_run, [h,]) )
for res in result_list:
res.get()
s.close()
执行效果如下