Import multiprocessing
Import Time,datetime
def ssh_cmd (host,cmdfile):
elog = open (' Error.log ', ' A + ')
log = open (' 7z.log ', "A +")
For CMD in open (Cmdfile). ReadLines ():
CMD = Cmd.strip ()
if not Len (CMD) or Cmd.startswith (' # '):
Continue
ssh = Paramiko. Sshclient ()
Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ())
Try:
Ssh.connect (hostname=host,port=22,username= ' root ', password= ' password ', timeout=10)
except Exception,e:
print ' connnet Fail%s '% host
elog.write ('%s '%host)
elog.close ()
Continue
Else:
print ' connnet Ok%s '% host
Stdin,stdout,stderr=ssh.exec_command (CMD)
Log.write (Stdout.read ())
log.close ()
starttime = Datetime.datetime.now ()
if __name__ = = "__main__":
os.remove (' 7z.log ')
os.remove (' Error.log ')
iplistfile= ' Iplist.txt '
cmdfile= ' config '
result = []
pool = multiprocessing. Pool (processes=8)
For host in Open (Iplistfile). ReadLines ():
Pool.apply_async (Ssh_cmd, (Host,cmdfile,))
pool.close ()
Pool.join ()
print ' Done '
endtime = Datetime.datetime.now ()
print "Time span", Endtime-starttime
This article is from the "I am a Little bird" blog, please make sure to keep this source http://2242558.blog.51cto.com/2232558/1545308
Python multiprocessing multi-process SSH