01.复制docx模板,重命名模板
02.ssh登录堡垒机,输入命令,获取结果写入txt文件
03.用txt文件构假的putty窗口,并截图
04.把截图插入docx文档
05.把生成txt文件,png文件删除
D:\Study\myproject\Python_auto_office\auto_check_new\main_ssh.py
#01.这个是主程序
# #-*-coding:utf-8-*-
'''000.导入包'''
#001.把字符串变列表的包
from posixpath import split
#002.SSH连接堡垒机,输入命令返回结果到txt文件的包
import util.exe_ssh as es
#003.构建截图窗口的包
import util.ssh_windows as sw
#004.截图的包
import util.screen_catch as sc
#005.向巡检报告插入图片的包
import util.insert_image as ii
#006.删除txt,png的包
import util.dele_file as df
#007.多线程的包
import threading
#008.时间的包
import time
#009.存储ip,端口,账号,密码,文件地址信息的类
import entity.ssh_info as si
'''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地址的列表
fwq_name = []
for value in ssh_obj.ip_dic.values():
fwq_name.append(value)
#115.存放需要命令的列表
fwq_command=ssh_obj.command_list
#116.服务器中文搜索的列表
fwq_search_name=[]
for key in ssh_obj.ip_dic.keys():
fwq_search_name.append(key)
#117.存放txt
fwq_pre_txt_address = ssh_obj.fwq_pre_txt_address
fwq_pre_img_address = ssh_obj.fwq_pre_img_address
'''200.业务代码'''
#连接堡垒机,SSH连接返回命结果
'''服务器的中文名'''
#fwq_search_name
'''服务器的命令'''
#fwq_command
'''服务器的ip地址'''
#fwq_name
'''命令结果输出到txt的目录地址'''
#fwq_pre_txt_address
#201.遍历服务器,把SSH连接堡垒机服务器输入命令返回结果写入txt文件中
es.comand2txt_loop(
ip,
port,
account,
pwd,
fwq_search_name,
fwq_command,
fwq_name,
fwq_pre_txt_address)
#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)
#补充命令,只有一个服务器用这个命令,所以单独写一个
#su - grid asmcmd lsdg
txt_address_11=fwq_pre_txt_address+fwq_name[3]+'-5.txt'
title_11="root@"+fwq_name[3]
img_path_11=fwq_pre_img_address+txt_address_11.split('.')[3]+'.png'
es.comand2txt_test(ip,
port,
account,
pwd,
fwq_search_name[3],
ssh_obj.command_list_plus[0],
txt_address_11)
a=sw.show_windows(txt_address_11,title_11,icon_path)
b=sc.screenshot_zone(img_path_11)
p1=threading.Thread(target=a,args=(None))
p2=threading.Thread(target=b,args=(None))
p2.start()
p1.start()
#往docx文档的标记插入图片
ii.table_insert_img(rename_path,img_path_11,'11-5')
#把生成png图片删除
df.del_file(fwq_pre_img_address)
#把生成txt文件删除
df.del_file(txt_path)
这个包用来SSH访问堡垒机获取命令返回结果
paramiko的使用
D:\Study\myproject\Python_auto_office\auto_check_new\util\exe_ssh.py
# coding: utf-8
'''000.导入包'''
#001.用来休眠1秒的包
import time
#002.这个是访问堡垒机的包
import paramiko
#003.迭代遍历对像的包
import itertools
'''001.方法'''
#101.删除特定字符串前的字符串的方法
def dele_before_str(longstring,s):
s=s+""
c=''.join(longstring.partition(s)[1:])
return c
#102.s1字符串在s2字符串出现的次数
def check(s1,s2):
return int(sum(map(lambda ch:s1.count(ch),s2))/2)
#103.命令结果生成txt文件
def comand2txt_test(
ip,
port,
account,
pwd,
server,
command,
txt_address
):
#私钥
pkey=paramiko.RSAKey.from_private_key_file("C:\\Users\\human\\.ssh\\id_rsa",password=pwd)
#paramiko SSH对象
client = paramiko.SSHClient()
#忘记了
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#连接堡垒机
client.connect(hostname=ip,port=port,username=account,pkey=pkey)
shell = client.invoke_shell()
#这个根据不同堡垒机,操作命令不同
shell.send(chr(0x2F))#/
#有时需要睡眠一点时间才能正常运行
time.sleep(1)
#搜索服务名 例如数据库服务1 这个堡垒机支持中文搜索
shell.send(server)
time.sleep(1)
shell.send(chr(0x0D))#回车
shell.send(chr(0x0D))#回车
time.sleep(1)
#输入命令,例如df -h
shell.send(command)
time.sleep(1)
shell.send(chr(0x0D))#回车
'''用来写到txt'''
#来一个死循环
while True:
time.sleep(1)
stdout = shell.recv(819200)
#编码格式变为utf-8
longstring = stdout.decode('utf-8')
# print(longstring)
#对字符串进行编辑
longstring = dele_before_str(
longstring,'[root').replace(
'--color=never','')
a=longstring.replace('-n 1 -b','')
if check("~]#",a)>=2:
with open(txt_address,'w') as f:
print(a,file = f)
# print(longstring)
break
def comand2txt_loop(
ip,
port,
account,
pwd,
fwq_search_name,
fwq_command,
fwq_name,
fwq_pre_txt_address):
fwq_imf=[]
for x in itertools.product(fwq_search_name,fwq_command,fwq_name):
'ip,port,account,pwd,server,command,txt_address'
fwq_imf.insert(0,ip)
fwq_imf.insert(1,port)
fwq_imf.insert(2,account)
fwq_imf.insert(3,pwd)
fwq_imf.insert(4,x[0])
fwq_imf.insert(5,x[1])
fwq_imf.insert(6,fwq_pre_txt_address+x[2]+"-"+str(fwq_command.index(x[1]))+".txt")
comand2txt_test(fwq_imf[0],fwq_imf[1],fwq_imf[2],fwq_imf[3],fwq_imf[4],fwq_imf[5],fwq_imf[6])
#当遍历到最后一个ip,最后一个命令时,跳出循环
#例如:
if x[2]=='192.168.200.12' and fwq_imf[5]=="iostat":
print("服务器名:")
print(x[2])
print("命令")
print(fwq_imf[5])
print("退出循环")
#退出循环
break
tkinker的使用、
D:\Study\myproject\Python_auto_office\auto_check_new\util\ssh_windows.py
'''000.导入包'''
#001.生成python交换窗口的包
import tkinter as tk
#002.屏幕截图的包
import util.screen_catch as sc
#003.用来体眠时间包
import time
#004.多线程包
import threading
#005.返回目录的文件名的包
import util.return_dir_file_name as rf
#006.左对齐的包
from turtle import left
#007.日期时间的包
from datetime import datetime
#file_path 打开路径
#title 窗口的标题
#icon_path 窗口图标的路径
'''100.方法'''
#101.txt的文件名,putty窗口的标题,putty窗口的图标
def show_windows(file_path,title,icon_path):
print("show_windows:"+str(datetime.now()))#显示一下时间
#打开txt文档读取内容
file_handle = open(file_path,mode = 'r')
content = file_handle.read()
root = tk.Tk()
#弹出窗口在最上层显示
root.attributes("-topmost",1)
root.after(3000, root.destroy)#放在这里才能生效 5秒后窗口关闭 关闭完一个窗口,可以再打开一个窗口
# root.after(5000, root.quit)#放在这里才能生效 5秒后窗口关闭
root.title(title)
#分辨率
root.geometry('3000x1800')
var = tk.StringVar()
label = tk.Label (root,
textvariable=var,
bg = 'black',
fg = 'white',
font = ('新宋体',12),
anchor="nw",
justify='left',
width = 2880,
height = 1800)
var.set(content)
label.pack()
root.iconbitmap(icon_path)
root.mainloop()
# sc.screenshot(img_path)
def show_windows_loop(fwq_pre_txt_address,
fwq_pre_img_address,
icon_path):
#获取目录中的文件名
return_file_list= rf.re_file_name_list(fwq_pre_txt_address)
print(return_file_list)
#2022_06_17 需要使用
for i in return_file_list:
#创建ssh窗口 截图 保存截图到img目录
#2022-06-17
ssh_txt=fwq_pre_txt_address+i
print(ssh_txt)
ssh_title="root@"+i.split('-')[0].replace('.txt','')
path_pre = fwq_pre_img_address
image_path = path_pre+i.replace('.txt','')+'.png'
# doc_image_name=i.replace('.txt','')+'.png'
# print(ssh_title)
print('图片的路径:'+image_path)
# #txt文件的路径 还有title "[email protected]"
print("文件名:"+i)
# #2022-06-01
a=show_windows(ssh_txt,ssh_title,icon_path)
# b=sc.screenshot(image_path)
b=sc.screenshot_zone(image_path)
p1=threading.Thread(target=a,args=(None))
p2=threading.Thread(target=b,args=(None))
p2.start()
time.sleep(1)
p1.start()
D:\Study\myproject\Python_auto_office\auto_check_new\util\screen_catch.py
截图的包
# pip install -U pillow 导入库
# 这是screen_catch.py文件
import time
from datetime import datetime
from PIL import ImageGrab
def screenshot(file_name_png):
ImageGrab.grab().save(file_name_png)
def screenshot_zone(file_name_png):
if "107-0.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,2095,1795)).save(file_name_png)
if "107-1.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1090,1460)).save(file_name_png)
if "107-2.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1360,445)).save(file_name_png)
if "107-3.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1410,1795)).save(file_name_png)
if "107-4.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1410,1795)).save(file_name_png)
if "107-5.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1420,1180)).save(file_name_png)
if "150-0.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,2090,1795)).save(file_name_png)
if "150-1.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1000,1300)).save(file_name_png)
if "150-2.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1335,480)).save(file_name_png)
if "150-3.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1440,1795)).save(file_name_png)
if "150-4.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1420,1200)).save(file_name_png)
if "150-5.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1420,1750)).save(file_name_png)
if "157-0.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,2130,1795)).save(file_name_png)
if "157-1.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,970,1350)).save(file_name_png)
if "157-2.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1345,480)).save(file_name_png)
if "157-3.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1440,1795)).save(file_name_png)
if "157-4.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1440,1200)).save(file_name_png)
if "11-0.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,2600,1795)).save(file_name_png)
if "11-1.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,970,1200)).save(file_name_png)
if "11-2.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1290,470)).save(file_name_png)
if "11-3.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1645,1795)).save(file_name_png)
if "11-4.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1320,1200)).save(file_name_png)
if "11-5.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,2270,500)).save(file_name_png)
if "12-5.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,2100,1755)).save(file_name_png)
if "12-0.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,2050,1795)).save(file_name_png)
if "12-1.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,960,1250)).save(file_name_png)
if "12-2.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1300,480)).save(file_name_png)
if "12-3.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1440,1795)).save(file_name_png)
if "12-4.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1440,1200)).save(file_name_png)
if "55-0.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,2270,500)).save(file_name_png)
if "55-1.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1050,895)).save(file_name_png)
if "55-2.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1600,480)).save(file_name_png)
if "55-3.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1440,1755)).save(file_name_png)
if "55-4.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1400,1700)).save(file_name_png)
if "55-5.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1100,1400)).save(file_name_png)
if "55-6.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1400,1800)).save(file_name_png)
if "55-7.png" in file_name_png:
ImageGrab.grab(bbox=(15,10,1100,1800)).save(file_name_png)