python3--批处理(执行、发送、获取)模块

全部改写了;
目前控制模块还没写;
发送模块 -- send.py

#!/usr/bin/python3
# -*- coding:utf-8 -*-
# date : 2020-09-27
# v3 : 修改格式 , 增加容错性

import os, shutil, argparse, sys, time, threading
import subprocess

def pre_fun():
    #BASE_DIR = os.getcwd()
    BASE_DIR = sys.path[0]
    os.chdir(BASE_DIR)
    if not os.path.isdir('tmp'):
        os.makedirs('tmp')
    TMP_DIR = os.path.join(BASE_DIR, 'tmp')
    IP_FILE = os.path.join(TMP_DIR, 'ip.txt')
    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--source", help="发送到远程服务器上的文件", type=str, default="None")
    parser.add_argument("-t", "--target", help="存到远程服务器上的路径", type=str, default="None")
    parser.add_argument("-i", "--ip_file", help="ip文件", type=str, default=IP_FILE)
    parser.add_argument("-ip", "--ip", help="ip", type=str, default=IP_FILE)
    args = parser.parse_args()
    target_dir = str(args.target)
    source_file = str(args.source)
    if target_dir == "None" or source_file == "None":
        print("-s参数, -t参数必须指定")
        sys.exit()
    gen_ip_fun(args.ip, args.ip_file, IP_FILE)
    return IP_FILE, source_file, target_dir


def gen_ip_fun(args_ip, args_ip_file, IP_FILE):
    if args_ip != IP_FILE and args_ip_file != IP_FILE:
        print("不能同时指定-i和-ip参数")
        sys.exit()
    elif args_ip != IP_FILE:
        f = open(IP_FILE, "w")
        f.write(args_ip)
        f.close()
    elif args_ip_file != IP_FILE:
        if os.path.isfile(args_ip_file):
            shutil.copy(str(args_ip_file), IP_FILE)
        else:
            print('ip file not exists!')
            sys.exit()


def run(IP_FILE, source_file, target_dir):
    ip_lists = []
    machines_info = []
    ip_file = open(IP_FILE, 'rb')
    for li in ip_file :
        if li and '#' not in str(li):
            machines_info.append(str(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 :
            for li_m in machines_info :
                if ip in li_m :
                    this_info = str(li_m)
                    this_info = str(li_m).split( '\'' )
                    this_info = this_info[1]
                    this_info = this_info.split( '\\' )
                    this_info = this_info[0]
            ip = str(ip).split('\'')
            ip = str(ip[1])
            ip = str(ip).split('\\')
            ip = str(ip[0])
            t_name = str(ip)
            t_name = myThread(this_info, 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('-----------------------------------------')


class myThread (threading.Thread):
    def __init__(self, this_info, name, source_file, target_dir):
        threading.Thread.__init__(self)
        self.this_info = this_info
        self.name = name
        self.source_file = source_file
        self.target_dir = target_dir
    def run(self):
        this_str="start :" + self.this_info
        print (this_str)
        try :
            command = "scp -o ConnectTimeout=5 -q -r " + str(self.source_file) + " root@" + str(self.name) + ":" + str(self.target_dir)
            res = subprocess.run(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding="utf-8",timeout=2)
        except Exception as e:
            error_info = "===> :" + str(self.this_info) + "\nerror info: " + str(e) + "\n\n"
            print(error_info)
            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 = "===> :" + self.this_info + '\n' + str(res) + '\n\n'
            print (this_str)


if __name__ == '__main__':
    IP_FILE, source_file, target_dir = pre_fun()
    run(IP_FILE, source_file, target_dir)

获取模块 -- get.py

#!/usr/bin/python3
# -*- coding:utf-8 -*-
# date : 2020-09-23
# v2 : add argparse
import os, shutil, argparse, sys, time, threading
import subprocess


def pre_fun():
    #BASE_DIR = os.getcwd()
    BASE_DIR = sys.path[0]
    os.chdir(BASE_DIR)
    if not os.path.isdir('tmp'):
        os.makedirs('tmp')
    TMP_DIR = os.path.join(BASE_DIR, 'tmp')
    IP_FILE = os.path.join(TMP_DIR, 'ip.txt')
    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--source", help="获取的文件的绝对路径+文件名", type=str, default="None")
    parser.add_argument("-t", "--target", help="存文件到本地服务器上的路径", type=str, default="None")
    parser.add_argument("-i", "--ip_file", help="ip文件", type=str, default=IP_FILE)
    parser.add_argument("-ip", "--ip", help="ip", type=str, default=IP_FILE)
    args = parser.parse_args()
    target_file = str(args.source)
    save_dir = str(args.target)
    if target_file == "None" or save_dir == "None":
        print("-s参数, -t参数必须指定")
        sys.exit()
    if not os.path.isdir(save_dir):
        os.makedirs(save_dir)
    gen_ip_fun(args.ip, args.ip_file, IP_FILE)
    return IP_FILE, target_file, save_dir


def gen_ip_fun(args_ip, args_ip_file, IP_FILE):
    if args_ip != IP_FILE and args_ip_file != IP_FILE:
        print("不能同时指定-i和-ip参数")
        sys.exit()
    elif args_ip != IP_FILE:
        f = open(IP_FILE, "w")
        f.write(args_ip)
        f.close()
    elif args_ip_file != IP_FILE:
        if os.path.isfile(args_ip_file):
            shutil.copy(args_ip_file, IP_FILE)
        else:
            print('ip file not exists!')
            sys.exit()


def run(IP_FILE, target_file, save_dir):
    ip_lists = []
    machines_info = []
    ip_file = open(IP_FILE, 'rb')
    for li in ip_file :
        if li and '#' not in str(li) :
            machines_info.append(str(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 :
            for li_m in machines_info :
                if ip in li_m :
                    this_info = str(li_m)
                    this_info = str(li_m).split( '\'' )
                    this_info = this_info[1]
                    this_info = this_info.split( '\\' )
                    this_info = this_info[0]
            ip = str(ip).split('\'')
            ip = str(ip[1])
            ip = str(ip).split('\\')
            ip = str(ip[0])
            this_save_dir = os.path.join(save_dir, str(ip))
            if not os.path.isdir(this_save_dir):
                os.makedirs(this_save_dir)
            t_name = str(ip)
            t_name = myThread(this_info, 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('-----------------------------------------')


class myThread (threading.Thread):
  def __init__(self, this_info, name, target_file, save_dir):
    threading.Thread.__init__(self)
    self.this_info = this_info
    self.name = name
    self.target_file = target_file
    self.save_dir = save_dir
  def run(self):
    this_str="start :" + self.this_info
    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.this_info + '\n' + str(res) + '\n\n'
      print (this_str)


if __name__ == '__main__':
    IP_FILE, target_file, save_dir = pre_fun()
    run(IP_FILE, target_file, save_dir)

执行模块 -- exec.py

#!/usr/bin/python3
# date : 2020-09-27
# v3 : add argparse
# author : xxwdll
# 说明 : 批量执行命令,需要与这台机器建立秘钥匹配
# 使用 : --help 查看
import os, shutil, argparse, sys, time, threading
import subprocess


# 定义变量IP_FILE, TMP_FILE
def pre_fun():
    # 生成tmp临时目录
    #BASE_DIR = os.getcwd()
    BASE_DIR = sys.path[0]
    os.chdir(BASE_DIR)
    if not os.path.isdir('tmp'):
        os.makedirs('tmp')
    # 定义临时目录, 临时ip文件, 临时执行文件
    TMP_DIR = os.path.join(BASE_DIR, 'tmp')
    IP_FILE = os.path.join(TMP_DIR, 'ip.txt')
    TMP_FILE = os.path.join(TMP_DIR, 'tmp.sh')
    parser = argparse.ArgumentParser()
    parser.add_argument("-i", "--ip_file", help="ip文件", type=str, default=IP_FILE)
    parser.add_argument("-ip", "--ip", help="ip", type=str, default=IP_FILE)
    parser.add_argument("-e", "--exec_file", help="执行文件", type=str, default=TMP_FILE)
    parser.add_argument("-c", "--command", help="执行文件", type=str, default=TMP_FILE)
    args = parser.parse_args()
    exec_file = args.exec_file
    exec_command = args.command
    gen_ip_fun(args.ip, args.ip_file, IP_FILE)
    gen_exec_fun(exec_file, exec_command, TMP_FILE)
    return IP_FILE, TMP_FILE


# 通过参数-i或ip; 生成ip文件
def gen_ip_fun(args_ip, args_ip_file, IP_FILE):
    if args_ip != IP_FILE and args_ip_file != IP_FILE:
        print("不能同时指定-i和-ip参数")
        sys.exit()
    elif args_ip != IP_FILE:
        f = open(IP_FILE, "w")
        f.write(args_ip)
        f.close()
    elif args_ip_file != IP_FILE:
        if os.path.isfile(args_ip_file):
            shutil.copy(args_ip_file, IP_FILE)
        else:
            print('ip file not exists!')
            sys.exit()


# 通过参数-e或-c; 生成执行文件
def gen_exec_fun(exec_file, exec_command, TMP_FILE):
    if str(exec_file) != str(TMP_FILE) and str(exec_command) != str(TMP_FILE):
        print("不能同时指定-c和-e参数")
        sys.exit()
    elif str(exec_file) != str(TMP_FILE):
        source = str(exec_file)
        target = TMP_FILE
        shutil.copy(source, target) 
    elif str(exec_command) != str(TMP_FILE):
        f = open(TMP_FILE, "w")
        f.write(str(exec_command))
        f.close()


# 读取配置文件 IP_FILE, TMP_FILE, 执行
def run(IP_FILE, TMP_FILE):
    #ip_file = open('ip.txt', 'rb')
    ip_lists = []
    ip_file = open(IP_FILE, 'rb')
    machines_info = []

    for li in ip_file :
        if li and '#' not in str(li):
            machines_info.append(str(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 :
            for li_m in machines_info :
                if ip in li_m:
                    this_info = str(li_m)
                    this_info = str(li_m).split( '\'' )
                    this_info = this_info[1]
                    this_info = this_info.split( '\\' )
                    this_info = this_info[0]
            t_ip = str(ip).split( '\'' )
            t_ip = t_ip[1]
            t_ip = str(t_ip).split( '\\' )
            t_ip = t_ip[0]
            t_exec_file = str(TMP_FILE)
            t_name = myThread(t_exec_file, t_ip, this_info)
            this_threads.append(t_name)
        [ thr.start() for thr in this_threads ]
        [ thr.join() for thr in this_threads ]
        print('-----------------------------------------')


class myThread (threading.Thread):
    def __init__(self, exec_file, name, tinfo):
        threading.Thread.__init__(self)
        self.exec_file = exec_file
        self.name = name
        self.tinfo = tinfo
    def run(self):
        this_ip = str(self.name)
        this_str="start :" + str(this_ip)
        print (this_str)
        try :
            command = "scp -o ConnectTimeout=5 -q -r " + str(self.exec_file)  + " root@" +  str(this_ip) + ":/home"
            res = subprocess.run(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding="utf-8",timeout=2)
        except Exception as e:
            error_info = "===> :" + str(self.tinfo) + "\nerror info: " + str(e) + "\n\n"
            print(error_info)
            return False
        else :
            res = os.popen(('ssh root@%s  -o ConnectTimeout=5 "cd /home;bash tmp.sh; rm -f tmp.sh"') % (str(this_ip))).read()
            this_str = "===> :" + str(self.tinfo) + "\n" + str(res) + "\n\n"
            print (this_str)
 
if __name__ == '__main__':
    IP_FILE, TMP_FILE = pre_fun()
    run(IP_FILE, TMP_FILE)

你可能感兴趣的:(python3--批处理(执行、发送、获取)模块)