python--批处理--批量获取模块

根据我之前的那个小改了下;
后面计划是将原本的主模块改为执行模块;
重新设计一个主模块,然后通过参数,指定各个子模块。

#!/usr/bin/python
# -*- coding:utf-8 -*-

import threading
import time
import sys
import os


class myThread (threading.Thread):
  def __init__(self, threadID, name, target_file, save_dir):
    threading.Thread.__init__(self)
    self.threadID = threadID
    self.name = name
    self.target_file = target_file
    self.save_dir = save_dir
  def run(self):
    this_str="start :" + self.name
    print (this_str)
    try :
      os.system(('scp -o ConnectTimeout=5 -q -r root@%s:%s %s') % (str(self.name), str(self.target_file), str(self.save_dir)))
    except :
      print ('connect fail !')
      return False
    else :
      this_save_dir=str(self.save_dir)+'/'+str(self.name)
      res = os.popen(('cd %s && ls -l') % (str(self.save_dir))).read()
      this_str = "==> " + self.name + '\n' + str(res) + '\n\n'
      print (this_str)

target_file = sys.argv[1]
save_dir = sys.argv[2]
os.system(('[ ! -d "%s" ] && mkdir -p "%s"') % (str(save_dir), str(save_dir)))

if __name__ == '__main__':
  ip_lists = []
  ip_file = open('ip.txt', 'rb')
  for li in ip_file :
    if li and '#' not in li :
      li = str(li).split()
      li = li[0]
      ip_lists.append(str(li))
  ip_file.close()

  #max_tasks = input('请输入并行个数:')
  max_tasks = 10
  while ip_lists :
    this_ip_lists = []
    for li in range(max_tasks) :
      if ip_lists :
        this_ip = ip_lists.pop(0)
        this_ip_lists.append(str(this_ip))
    this_threads = []
    for ip in this_ip_lists :
      this_save_dir=str(save_dir)+'/'+str(ip)
      os.system(('[ ! -d "%s" ] && mkdir -p "%s"') % (str(this_save_dir), str(this_save_dir)))
      t_name = str(ip)
      t_name = myThread(t_name, t_name, target_file, this_save_dir)
      this_threads.append(t_name)
    [ thr.start() for thr in this_threads ]
    [ thr.join() for thr in this_threads ]
    print('-----------------------------------------')

第一个参数为远程机器上要获取的文件,第二个参数为想要保存到哪里


python--批处理--批量获取模块_第1张图片

你可能感兴趣的:(python--批处理--批量获取模块)