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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355