Two-Step Vertification required:

Please enter the mobile app OTPverification code:

01.因为巡检的服务器要双因子认证登录,也就是登录堡垒机时还要输入验证码。这对我的巡检查服务器的工作带来了不便。它的机制是每一次登录,算一次会话,每次会话要输入一次验证码。

总共5台服务器,每个服务器执行5个常规命令,其中一个服务器执行一个特殊命令 5*5+1=26
那么我就要输入26次验证码,验证码每30秒变更一次。每个验证码能用2,3次。

02.验证码肯定要输入的,那么就输入一次,把所有命令操作打包成一次会话执行所有命令,命令包含回车等操作符。

把命放在列表中,当然用循环拼接字符串,一个一个写也可以,但太长可能会写错。

不讲这么多,上脚本主题

# #-*-coding:utf-8-*-
'''000.导入包'''

# from posixpath import split 
#001.SSH连接堡垒机,输入命令返回结果到txt文件的包
import util.exe_ssh as es 
#002.构建截图窗口的包
import util.ssh_windows as sw
#003.向巡检报告插入图片的包
import util.insert_image as ii
#004.删除txt,png的包
import util.dele_file as df
#005.时间的包
import time
#006.存储ip,端口,账号,密码,文件地址信息的类
import entity.ssh_info as si
#007.创建命令列表的包
import util.create_command_list as ccl

'''100.变量'''
#101.获取当天的年月日
current_time = time.strftime('%Y-%m-%d',time.localtime(time.time()))
#102.我所有登录信息话在ssh_access类里面,通过对象调用变量参数
ssh_obj = si.ssh_access()
#103.单纯文件名
file_name = ssh_obj.file_name
#104.文件的后序docx#
seq_name = ssh_obj.seq_name
#105.存放巡检报告的目录
default_path = ssh_obj.default_path
#106.巡检报告模板的地址
origen_path = default_path+"\\"+file_name+seq_name
#107.重命名巡检模板的巡检报告的地址
rename_path = default_path+"\\"+current_time+"\\"+file_name+current_time+seq_name
#108.putty,因为做一个假的putty窗口,putty图标的位置
icon_path=ssh_obj.icon_path
#109.存放命令返回结果txt文件的目录位置
txt_path=ssh_obj.txt_path
#110.访问堡垒机的ip地址
ip = ssh_obj.ip
#111.访问堡垒机的端口
port=ssh_obj.port
#112.访问堡垒机的账号
account=ssh_obj.account
#113.访问堡垒机的密码
pwd=ssh_obj.pwd
#114.服务器ip地址的列表
#115存ip的列表
fwq_name = [] 
for value in ssh_obj.ip_dic.values():
    fwq_name.append(value)
'''print("fwq_name的内容")'''
'''print(fwq_name)'''
#116.存放需要命令的列表
fwq_command=ssh_obj.command_list
'''print("fwq_command的内容")'''
'''print(fwq_command)'''
#117.服务器中文搜索的列表
fwq_search_name=[]
for key in ssh_obj.ip_dic.keys():
    fwq_search_name.append(key)
'''print("fwq_search_name的内容")'''
'''print(fwq_search_name)'''
#118.存放写入txt的目录
fwq_pre_txt_address = ssh_obj.fwq_pre_txt_address
'''print("fwq_pre_txt_address的内容")'''
'''print(fwq_pre_txt_address)'''
#119.存放截图的目录
fwq_pre_img_address = ssh_obj.fwq_pre_img_address
#120.单次会话所有输入命令的列表
full_fwq_name_command_list=ccl.get_fwq_full_name_command_list(fwq_search_name,fwq_command)

'''200.业务代码'''
#连接堡垒机,SSH连接返回命结果到txt文件
es.command2txt(ip,             #登录堡垒机的ip地址
               port,           #登录堡垒机的端口
               account,        #登录堡垒机的账号
               pwd,            #登录堡垒机的密码
               fwq_name,       #服务器ip地址的列表
               full_fwq_name_command_list,  #单次会话所有输入命令的列表
               fwq_pre_txt_address  #存放写入txt的目录
               )

#202.遍历截图
sw.show_windows_loop(fwq_pre_txt_address,fwq_pre_img_address,icon_path)    
#203.遍历插入图片
ii.table_insert_img_loop(rename_path,fwq_pre_img_address)    
#把生成png图片删除
df.del_file(fwq_pre_img_address)     
#把生成txt文件全部删除
df.del_file(txt_path)

我主要是对正面的包进行修改

#001.SSH连接堡垒机,输入命令返回结果到txt文件的包

import util.exe_ssh as es

# coding: utf-8
'''000.导入包'''
#001.用来休眠1秒的包
import time
#002.这个是访问堡垒机的包
import paramiko
#003.获取目录中所有文件名的包
import util.return_dir_file_name as rdf
#004.用于删除文件
import os 
#005.创建相同长度的数字字符串列表的包
import util.create_number_order as cno
#006.删除特定字符串前的字符串的包
import util.delete_before_string as dbs
#007.操作txt文件的包,里面写了两个方法,一个是把两个txt文件合并,一个是地址拼接前面加一些东西
import util.txt_util as tu

'''100.变量'''
#101.服务器账号前序
account_imf=['[root@oaapp02 ~]# ',
            '[root@oaapp01 ~]# ',
            '[root@bang01 ~]# ',
            '[root@rac1 ~]# ',
            '[root@rac2 ~]# ']
#102.txt的文件名列表,用于对指定txt文件进行操作
keywords_01 = ["01", "12", "23","34","45",'56']
keywords_2=["03","05","07","09"]
keywords_3=["14","16","18","20"]
keywords_4=["25","27","29","31"]
keywords_5=["36","38","40","42"]
keywords_6=["47","49","51","53"]

'''200.方法'''
#201.把命令执行结果写入txt文件的方法
def command2txt(ip,                 #ip不变的
                port,               #端口不变的
                account,            #用户名不变的
                pwd,                #密码不变的
                fwq_name,         #服务器ip的列表
                commands,         #不是单个命令,而是接收一个命令列表   
                txt_path,             
                txt_address         #txt的地址列表
                ):
    
    #202.获取私钥
    pkey=paramiko.RSAKey.from_private_key_file("C:\\Users\\human\\.ssh\\id_rsa",password=pwd)
    #203.paramiko SSH对象
    client = paramiko.SSHClient()
    #204.自动将服务器的公钥添加到known_hosts文件,并接受公钥。
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    #205.连接堡垒机
    client.connect(hostname=ip,port=port,username=account,pkey=pkey,timeout=60)
    #206.创建发送命令的对象
    shell = client.invoke_shell()
    #207.生成00-99的字符串列表
    number_list=cno.create_hundred_order_list(99)
    # print(number_list)
    #208.设置计数器count,拼设置初值为0
    count=0
    #209.遍历里面的命令
    for comand in commands:
        #210.用下标控制获取数字
        n=number_list[count]
        # print(n)
        #211.发送命令
        shell.send(comand)
        #212.如果准备就pass跳过
        while not shell.recv_ready():
            pass
        #213.睡眠0.5秒,模仿人操作
        time.sleep(0.5)
        #214.如果命令是'/',则跳过
        if comand == '/':
            continue
        #215.如果命令是搜索的堡垒机的字符串,则跳过
        elif comand in ('应用服务器02','应用服务器03','应用服务器01','数据库1','数据库2'):
            continue
        #216.过滤完执行下面的
        else:
            #217.接收命令执行结果
            stdout = shell.recv(10240)
            #218.对结果进行解码
            longstring = stdout.decode('utf-8')
            #219.如果结果包含"连用云",则跳过
            if "连用云" in longstring:
               continue
            #220.如果结果包含"exit",则跳过
            elif "exit" == longstring:
               continue
            else:
                #221.将结果写入txt文件
                with open(txt_address+n+".txt",'w') as f:
                    print(longstring,file = f)
                #222.计数器加1
                count+=1

    #223.获取目录中的文件名列表    
    file_name_list=rdf.re_file_name_list(txt_path)
    # print(file_name_list)
    #224.删除开头没有用的txt文件
    for f in file_name_list:
        print("f的值")
        print(f)
        keywords = ["00", "11", "22","33","44","55"]
        if any(keyword in f for keyword in keywords):
            os.remove(txt_path+"\\"+f)
    #225.删除完文件再获取目录中的文件名列表
    file_name_list=rdf.re_file_name_list(txt_path)
    #226.删除登录每个服务器txt文件开头的一些内容
    for f in file_name_list:
        # print("f的值")
        # print(f)
        #227.
        if any(keyword in f for keyword in keywords_01):
            # os.remove(txt_path+"\\"+f)
            with open(txt_path+"\\"+f, 'r') as file:
                content = file.read()
                if '56' in f:
                    content = dbs.dele_before_str(content,'[root')
                    with open(txt_path+"\\"+f, 'w') as file:
                        file.write(content)

                else: 
                    content = dbs.dele_before_str(content,'[root').replace("--color=never","")
                    with open(txt_path+"\\"+f, 'w') as file:
                        file.write(content)
        
       #删除完再获取
    for f in file_name_list:
        if any(keyword in f for keyword in keywords_2):  
                with open(txt_path+"\\"+f, 'r') as file:
                    content = file.read()
                    content = account_imf[0]+content
                with open(txt_path+"\\"+f, 'w') as file:
                    file.write(content)
    for f in file_name_list:
        if any(keyword in f for keyword in keywords_3):  
                with open(txt_path+"\\"+f, 'r') as file:
                    content = file.read()
                    content = account_imf[1]+content
                with open(txt_path+"\\"+f, 'w') as file:
                    file.write(content)
    for f in file_name_list:
        if any(keyword in f for keyword in keywords_4):  
                with open(txt_path+"\\"+f, 'r') as file:
                    content = file.read()
                    content = account_imf[2]+content
                with open(txt_path+"\\"+f, 'w') as file:
                    file.write(content)
    for f in file_name_list:
        if any(keyword in f for keyword in keywords_5):  
                with open(txt_path+"\\"+f, 'r') as file:
                    content = file.read()
                    content = account_imf[3]+content
                with open(txt_path+"\\"+f, 'w') as file:
                    file.write(content)
    for f in file_name_list:
        if any(keyword in f for keyword in keywords_6):  
                with open(txt_path+"\\"+f, 'r') as file:
                    content = file.read()
                    content = account_imf[4]+content
                with open(txt_path+"\\"+f, 'w') as file:
                    file.write(content)
   
    file_name_list=rdf.re_file_name_list(txt_path)
    
    # print("fwq_name的值")                
    # print(fwq_name)

    for ip_name in fwq_name:
    
        if ip_name==fwq_name[0]:
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[0]),
                       tu.add_pre_txt_address(file_name_list[1]),
                       tu.add_pre_txt_address(ip_name+"-0.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[2]),
                       tu.add_pre_txt_address(file_name_list[3]),
                       tu.add_pre_txt_address(ip_name+"-1.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[4]),
                       tu.add_pre_txt_address(file_name_list[5]),
                       tu.add_pre_txt_address(ip_name+"-2.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[6]),
                       tu.add_pre_txt_address(file_name_list[7]),
                       tu.add_pre_txt_address(ip_name+"-3.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[8]),
                       tu.add_pre_txt_address(file_name_list[9]),
                       tu.add_pre_txt_address(ip_name+"-4.txt"))
        elif ip_name==fwq_name[1]:
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[10]),
                       tu.add_pre_txt_address(file_name_list[11]),
                       tu.add_pre_txt_address(ip_name+"-0.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[12]),
                       tu.add_pre_txt_address(file_name_list[13]),
                       tu.add_pre_txt_address(ip_name+"-1.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[14]),
                       tu.add_pre_txt_address(file_name_list[15]),
                       tu.add_pre_txt_address(ip_name+"-2.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[16]),
                       tu.add_pre_txt_address(file_name_list[17]),
                       tu.add_pre_txt_address(ip_name+"-3.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[18]),
                       tu.add_pre_txt_address(file_name_list[19]),
                       tu.add_pre_txt_address(ip_name+"-4.txt"))
        elif ip_name==fwq_name[2]:
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[20]),
                       tu.add_pre_txt_address(file_name_list[21]),
                       tu.add_pre_txt_address(ip_name+"-0.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[22]),
                       tu.add_pre_txt_address(file_name_list[23]),
                       tu.add_pre_txt_address(ip_name+"-1.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[24]),
                       tu.add_pre_txt_address(file_name_list[25]),
                       tu.add_pre_txt_address(ip_name+"-2.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[26]),
                       tu.add_pre_txt_address(file_name_list[27]),
                       tu.add_pre_txt_address(ip_name+"-3.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[28]),
                       tu.add_pre_txt_address(file_name_list[29]),
                       tu.add_pre_txt_address(ip_name+"-4.txt"))
        elif ip_name==fwq_name[3]:
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[30]),  
                      tu.add_pre_txt_address(file_name_list[31]),
                       tu.add_pre_txt_address(ip_name+"-0.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[32]),
                       tu.add_pre_txt_address(file_name_list[33]),
                       tu.add_pre_txt_address(ip_name+"-1.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[34]),
                       tu.add_pre_txt_address(file_name_list[35]),
                       tu.add_pre_txt_address(ip_name+"-2.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[36]),
                       tu.add_pre_txt_address(file_name_list[37]),
                       tu.add_pre_txt_address(ip_name+"-3.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[38]),
                       tu.add_pre_txt_address(file_name_list[39]),
                       tu.add_pre_txt_address(ip_name+"-4.txt"))
        
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[-3]),
                       tu.add_pre_txt_address(file_name_list[-2]),
                       tu.add_pre_txt_address(ip_name+"-5.txt"))
            
        elif ip_name==fwq_name[4]:
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[40]),
                       tu.add_pre_txt_address(file_name_list[41]),
                       tu.add_pre_txt_address(ip_name+"-0.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[42]),
                       tu.add_pre_txt_address(file_name_list[43]),
                       tu.add_pre_txt_address(ip_name+"-1.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[44]),
                       tu.add_pre_txt_address(file_name_list[45]),
                       tu.add_pre_txt_address(ip_name+"-2.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[46]),
                       tu.add_pre_txt_address(file_name_list[47]),
                       tu.add_pre_txt_address(ip_name+"-3.txt"))
            tu.conbine_txt(tu.add_pre_txt_address(file_name_list[48]),
                       tu.add_pre_txt_address(file_name_list[49]),
                       tu.add_pre_txt_address(ip_name+"-4.txt"))

        #删除00-55的txt文
    for f in file_name_list:
        print("f的值")
        print(f)
        # keywords = ["00", "11", "22","33","44","55"]
        if any(keyword in f for keyword in number_list):
            os.remove(txt_path+"\\"+f)

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部