您的位置:首页 > 编程语言 > Python开发

python multiprocessing多进程 ssh

2014-08-26 17:45 246 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息