美文网首页Python
python--批处理--批量发送文件

python--批处理--批量发送文件

作者: w_dll | 来源:发表于2020-09-01 19:21 被阅读0次

目前写这个的目的是为了实现,可以批处理发送的一个模块;
后期将各个模块整合到一起,实现更加丰富的功能,类似于ansible;
这次又增加了一些代码的强壮性;
使用方法
./send 源文件 目标地址
后期完全写完后会传到 github,并且写说明文档;
目前这样就行。

源码如下:

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

import threading
import time
import sys
import os


class myThread (threading.Thread):
  def __init__(self, threadID, name, source_file, target_dir):
    threading.Thread.__init__(self)
    self.threadID = threadID
    self.name = name
    self.source_file = source_file
    self.target_dir = target_dir
  def run(self):
    this_str="start :" + self.name
    print (this_str)
    try :
      os.system(('scp -o ConnectTimeout=5 -q -r %s root@%s:%s') % (str(self.source_file), str(self.name), str(self.target_dir)))
    except :
      print ('connect fail !')
      return False
    else :
      res = os.popen(('ssh -o ConnectTimeout=5 root@%s "cd %s;pwd && ls -l|grep -w %s"') % (str(self.name), str(self.target_dir), str(self.source_file))).read()
      this_str = "end :" + self.name + '\n' + str(res) + '\n\n'
      print (this_str)

source_file = sys.argv[1]
target_dir = sys.argv[2]
source = str(source_file)
target = str(target_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 :
      t_name = str(ip)
      t_name = myThread(t_name, t_name, source_file, target_dir)
      this_threads.append(t_name)
    [ thr.start() for thr in this_threads ]
    [ thr.join() for thr in this_threads ]
    print('-----------------------------------------')

使用结果


相关文章

网友评论

    本文标题:python--批处理--批量发送文件

    本文链接:https://www.haomeiwen.com/subject/hdxbsktx.html