标签:python multiprocessing多进程 ssh
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
本文出自 “我是一只小小鸟” 博客,请务必保留此出处http://2242558.blog.51cto.com/2232558/1545308
标签:python multiprocessing多进程 ssh
原文地址:http://2242558.blog.51cto.com/2232558/1545308