Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from ctypes import alignment
- from tkinter import *
- import os
- from tkinter import messagebox
- from tkinter import PhotoImage
- import win32com.client as win32
- from netmiko import ConnectHandler
- from datetime import datetime
- import time
- import os
- import threading
- import pythoncom
- import telnetlib
- def acerca_de():
- messagebox.showinfo("ACERCA DE...","""by Gustavo P. Fernandez 2022
- comandos = []
- ruta = (os.path.abspath(os.getcwd()))
- root = Tk()
- barra_de_menu = Menu(root)
- menu_ayuda = Menu(root,barra_de_menu, tearoff=0)
- menu_ayuda.add_command(label="Acerca de...", command=acerca_de)
- barra_de_menu.add_cascade(label="Ayuda", menu=menu_ayuda)
- root.config(background="light gray",menu=barra_de_menu)
- root.geometry("610x550")
- root.title("enviar mail")
- root.iconbitmap("Logo2.ico")
- def recolectar():
- putty = r.get()
- comandos = []
- if var1.get():
- comandos.append("show version")
- if var2.get():
- comandos.append("show inventory")
- if var3.get():
- comandos.append("show log")
- a = caja_comando_manual.get()
- if a != "":
- a.strip()
- comandos.append(a)
- if var4.get():
- comandos.append("show running-config")
- address = direccion_host_box.get()
- user = usuario_box.get()
- psw = password_box.get()
- hacia = direcciones_destinatario_box.get()
- repetir = repetir_box.get()
- cantidad = cant_veces_box.get()
- cuerpo = cuerpo_c.get("1.0",END)
- asunto = asunto_e.get()
- nombre2 = ""
- try:
- repetir = int(repetir)
- cantidad = int(cantidad)
- except ValueError:
- messagebox.showerror("ERROR","el campo cantidad de veces o repetir debe ser un numero entero mayor que 0")
- if address == "" or user == "" or psw == "" or hacia == "" or repetir == "":
- messagebox.showerror("ERROR","los campos no deben estar vacios")
- if repetir <= 0:
- messagebox.showerror("ERROR","el campo numero debe ser un numero entero mayor que 0")
- def mandar_mail():
- pythoncom.CoInitialize()
- def conectarse():
- if putty == "SSH":
- try:
- swmsc = ConnectHandler(device_type="cisco_ios", ip=address, username=user, password=psw,secret=psw)
- except:
- messagebox.showerror("ERROR","El usuario,contraseƱa o puerto es incorrecto. por favor verifique")
- exit()
- swmsc.enable()
- timestr = datetime.now().strftime("%d/%m/%Y-%H:%M:%S")
- timestr2 = datetime.now().strftime("%d-%m-%Y %H-%M-%S")
- for x in comandos:
- a = swmsc.send_command(x)
- command = f"{x}"
- nombre = swmsc.send_command("show running | include hostname")
- nombre = nombre.replace("hostname ","")
- nombre = nombre.replace("\n. @@@@@@ @@@@@@ @@@@@@@ Nodo: $(hostname)","")
- nombre2 = f"{nombre} {timestr2}.txt"
- r = open(nombre2,"a")
- r.writelines(f"{command}:\n{a}\n########################################################\n")
- r.close()
- swmsc.disconnect()
- elif putty == "TELNET":
- try:
- timestr = datetime.now().strftime("%d/%m/%Y-%H:%M:%S")
- timestr2 = datetime.now().strftime("%d-%m-%Y %H-%M-%S")
- for x in comandos:
- tel = telnetlib.Telnet(address)
- tel.read_until(b"sername: " or b"ogin: ")
- tel.write(user.encode("ascii")+b"\n")
- tel.read_until(b"assword: ",timeout=5)
- tel.write(psw.encode("ascii")+b"\n")
- tel.write(b"enable\n")
- tel.read_until(b"assword: ",timeout=5)
- tel.write(psw.encode("ascii")+b"\n")
- nombre = tel.read_until(b"#",timeout=5)
- nombre = nombre.decode("utf-8")
- nombre = nombre.replace("*","")
- nombre = nombre.replace("#","")
- nombre = nombre.strip()
- if x == "show running-config":
- comando = x+"\n "
- tel.write(comando.encode("ascii")+b"\n")
- time.sleep(2)
- output = tel.set_debuglevel(1000000000)
- output = tel.read_until(b"#")
- output = output.decode("utf-8")
- else:
- comando = x+"\n "
- tel.write(comando.encode("ascii")+b"\n")
- time.sleep(2)
- output = tel.set_debuglevel(1000000000)
- output = tel.read_until(b"#")
- output = output.decode("utf-8")
- nombre2 = f"{nombre} {timestr2}.txt"
- r = open(nombre2,"a")
- r.writelines(f"{output}:\n")
- tel.close()
- r.close()
- except:
- messagebox.showerror("ERROR","El usuario,contraseƱa o puerto es incorrecto. por favor verifique")
- exit()
- ################################ conexion outlook
- body = f"{timestr}\n{cuerpo}"
- outlook = win32.Dispatch('outlook.application')
- mail = outlook.CreateItem(0)
- try:
- mail.To = hacia
- except:
- messagebox.showerror("ERROR","Outlook no reconoce alguno de los nombres DE DESTINO")
- exit()
- mail.Subject = asunto
- mail.Body = body
- # mail.HTMLBody = '<h2>HTML Message body</h2>' #this field is optional
- # To attach a file to the email (optional):
- # attachment = nombre2
- mail.Attachments.Add(f"{ruta}\\{nombre2}")
- mail.Send()
- if ping.get():
- if os.name == "nt":
- response = os.system("ping " + address + " -n 1")
- elif os.name == "posix":
- response = os.system("ping " + address + " -c 1")
- if response != 0:
- messagebox.showerror("ERROR","El Host no responde Ping")
- exit()
- else:
- conectarse()
- else:
- conectarse()
- for x in range(1,cantidad+1):
- mandar_mail()
- time.sleep(repetir)
- def hilo():
- t1 = threading.Thread(target=recolectar)
- t1.start()
- ##############FRAMES#################
- marco1 = LabelFrame(root,text="datos del equipo",bg="light gray",borderwidth=5,pady=10,labelanchor="n")
- marco1.place(x=10,y=10)
- marco3 = LabelFrame(root,text="datos a recolectar",bg="light gray",borderwidth=5,pady=25,padx=5,labelanchor="n")
- marco3.place(x=210,y=80)
- b_procesar = Button(root,text="Procesar",command=hilo,borderwidth=1,padx=5,pady=5)
- b_procesar.place(x=250,y=10)
- marco2 = LabelFrame(root,text="Mail",bg="light gray",borderwidth=5,pady=25,padx=5,labelanchor="n")
- marco2.place(x=400,y=10)
- img = f"{ruta}\\logicalis-logo.png"
- imagen= PhotoImage(file=img)
- etiqueta_img = Label(root, image=imagen)
- etiqueta_img.place(x=0,y=410)
- # marco1
- direccion_host_et = Label(marco1,text="ip o hostname")
- direccion_host_et.pack()
- direccion_host_box = Entry(marco1)
- direccion_host_box.pack()
- direccion_host_et.config(background="light gray")
- usuario_et = Label(marco1,text="usuario")
- usuario_et.pack()
- usuario_et.config(background="light gray")
- usuario_box = Entry(marco1)
- usuario_box.pack()
- password_et = Label(marco1,text="password")
- password_et.pack()
- password_et.config(background="light gray")
- password_box = Entry(marco1,show="*")
- password_box.pack()
- direcciones_destinatario_et = Label(marco1,text="e-mail destino")
- direcciones_destinatario_et.pack()
- direcciones_destinatario_et.config(background="light gray")
- direccion_host_et.config(background="light gray")
- direcciones_destinatario_box = Entry(marco1)
- direcciones_destinatario_box.pack()
- repetir_et = Label(marco1,text="repetir cada... segundos")
- repetir_et.pack()
- repetir_et.config(background="light gray")
- repetir_box = Entry(marco1)
- repetir_box.pack()
- ##################marco3
- cant_veces = Label(marco1,text="repetir ... veces")
- cant_veces.pack()
- cant_veces.config(background="light gray")
- cant_veces_box = Entry(marco1)
- cant_veces_box.pack()
- r = StringVar()
- r.set("SSH")
- Radiobutton(marco1,background="light gray",text="SSH/puerto22",variable=r,value="SSH").pack(anchor="w")
- Radiobutton(marco1,background="light gray",text="TELNET/puerto23",variable=r,value="TELNET").pack(anchor="w")
- comandos = []
- var1 = IntVar()
- var2 = IntVar()
- var3 = IntVar()
- var4 = IntVar()
- var5 = IntVar()
- ping = BooleanVar()
- p = Checkbutton(marco1,text="comprobacion por ping",variable=ping, onvalue=True, offvalue=False,background="light gray").pack(anchor="w")
- ping.set(True)
- chk_sh_log = Checkbutton(marco3,text="show log",variable=var1, onvalue=1, offvalue=0)
- chk_sh_log.pack(anchor="w")
- chk_sh_log.config(background="light gray")
- chk_sh_ver = Checkbutton(marco3,text="show version",variable=var2, onvalue=1, offvalue=0)
- chk_sh_ver.pack(anchor="w")
- chk_sh_ver.config(background="light gray")
- chk_sh_inventory = Checkbutton(marco3,text="show inventory",variable=var3, onvalue=1, offvalue=0)
- chk_sh_inventory.pack(anchor="w")
- chk_sh_inventory.config(background="light gray")
- chk_sh_run = Checkbutton(marco3,text="show running-config",variable=var4, onvalue=1, offvalue=0)
- chk_sh_run.pack(anchor="w")
- chk_sh_run.config(background="light gray")
- texto_comando_manual = Label(marco3,text="introduzca comando:")
- texto_comando_manual.pack()
- texto_comando_manual.config(background="light gray")
- caja_comando_manual = Entry(marco3,state="normal")
- caja_comando_manual.pack()
- ################### marco2
- asunto_et = Label(marco2,text="Asunto:",pady=5)
- asunto_et.pack(anchor="w")
- asunto_e = Entry(marco2)
- asunto_e.pack(anchor="w")
- asunto_et.config(background="light gray")
- cuerpo_e = Label(marco2,text="Cuerpo:",pady=5)
- cuerpo_e.pack(anchor="w")
- cuerpo_e.config(background="light gray")
- cuerpo_c = Text(marco2,width=20,height=15)
- cuerpo_c.pack()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement