[ implementation: multi-process Paramiko]
1.server_list.txt: Format HOST PORT USER PASSWD
localhost root root
193.168.49.144 root root
2.get_server_list.py
Server_list_file = "Server_list.txt"
Def get_serverlist_dic ():
Server_dic = {}
f = File (Server_list_file)
For line in F.readlines ():
If Len (Line.strip ()) = = 0:break
Server_dic[line.split () [0]] = line.split () [1:]
F.close ()
Return Server_dic
Server_dic = Get_serverlist_dic ()
#print Server_dic
3.single_paramiko.py
#!/usr/bin/env python
#encoding: UTF8
Import Paramiko
Import Sys,os,time
Host = Sys.argv[1]
Port = Int (sys.argv[2])
user = Sys.argv[3]
Password = sys.argv[4]
cmd = str (sys.argv[5])
msg = "---------result:%s----------"%host
P_client = Paramiko. Sshclient () #绑定实例
P_client.load_system_host_keys () #加载本机HOST主机文件
P_client.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Try
P_client.connect (host,port,user,password,timeout=5)
Stdin,stderr,stdout = P_client.exec_command (cmd)
Cmd_result = Stdout.read (), Stderr.read ()
Print msg
For line in Cmd_result:
Print line,
Except Paramiko. Authenticationexception,e:
Print msg
Print "message:" + str (e)
Sys.exit ()
Except Paramiko. Badhostkeyexception,e:
Print msg
Print "Bad host key" + STR (e)
P_client.close ()
4.multi_async.py
Import multiprocessing
Import Os,sys,time
Import Get_server_list
cmd = sys.argv[1]
result = []
def runcmd (H,port,user,passwd,command):
cmd = "Python single_paramiko.py%s%s%s%s%s"% (H,port,user,passwd,command)
Print cmd
Os.system (CMD)
p = multiprocessing. Pool (processes=250)
Server_dic = Get_server_list.server_dic
For host,values in Server_dic.items ():
Ssh_port = Values[0]
Username = values[1]
Password = values[2]
Result.append (P.apply_async (Runcmd, (Host,ssh_port,username,password,cmd,)))
For res in result:
Res.get (timeout=35)
5. Actual operation
[[email protected] multiprocess]# python multi_async.py ls
Python single_paramiko.py 193.168.49.144 root ls
Python single_paramiko.py localhost root root ls
---------Result:localhost----------
Aaa.a
Anaconda-ks.cfg
Desktop
Documents
Downloads
Install.log
Install.log.syslog
.....
.....
---------result:193.168.49.144----------
Anaconda-ks.cfg
Desktop
Documents
Downloads
Install.log
Install.log.syslog
.....
.....
Python's multiprocessing (b): multiprocessing. Pool