艾辉 发表于 2018-8-7 11:50:49

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
页: [1]
查看完整版本: python multiprocessing多进程 ssh