Инструмент устранения неполадок оборудования Cisco

Я сетевой инженер и начал играть с Python, чтобы сократить время простоя в работе. У нас нет автоматизации, поэтому я экспериментировал с созданием графического интерфейса, чтобы запускать простые команды для нашего оборудования и выводить результат в отдельном окне. Главное окно (корень) остается активным все время, так что вы можете изменить подключенное устройство, учетные данные, IP-адрес и т. Д., Что позволяет «быстрее» обнаруживать проблемы.

Я никогда не брал какие-либо классы Python, и каждая часть кода была подобрана на лету из разных источников, но я собрал достаточно, чтобы понять, что мне нужно.

У меня действительно нет проблем с кодом, кроме неработающих полос прокрутки, но я знаю, почему это не проблема на данный момент.

Поскольку в моей команде нет никого, кто мог бы сказать мне, хорош этот код или плох, мне было любопытно посмотреть, может ли кто-нибудь просто взглянуть на код и сказать мне, что они думают.

#Imported packages. You may need to run "pip install <package>" where <package> is the package listed below. If you are
#unsure how to install Python packages then try looking at instructions here:
# https://packaging.python.org/tutorials/installing-packages/#
from netmiko import (
    ConnectHandler,
    NetmikoTimeoutException,
    NetmikoAuthenticationException,
)
from getpass import getpass
from tkinter import *

#Sets up the main window#
root = Tk()
root.title("Cisco Troubleshooter")
root.configure(background="black")
root.iconbitmap('cisco.ico')

def on_device_click(event):
    """function that gets called whenever entry is clicked"""
    if device.get() == "Enter the IP/Hostname you want to connect to":
        device.delete(0, "end")  # delete all the text in the entry
        device.insert(0, '')  # Insert blank for user input
        device.config(fg='black')


#Displays red text if the box is clicked on, not filled out, then exited#
def on_device_focusout(event):
    if device.get() == '':
        device.insert(0, "Enter the IP/Hostname you want to connect to")
        device.config(fg="red")


def on_UN_click(event):
    if UN.get() == "Enter your username":
        UN.delete(0, "end")  # delete all the text in the entry
        UN.insert(0, '')  # Insert blank for user input
        UN.config(fg='black')


#Displays red text if the box is clicked on, not filled out, then exited#
def on_UN_focusout(event):
    if UN.get() == '':
        UN.insert(0, "Enter your username")
        UN.config(fg="red")


def on_PW_click(event):
    if PW.get() == "*":
        PW.delete(0, "end")  # delete all the text in the entry
        PW.insert(0, '')  # Insert blank for user input
        PW.config(fg='black')

    elif PW.get() == "Enter your password":
        PW.delete(0, "end")  # delete all the text in the entry
        PW.insert(0, '')  # Insert blank for user input
        PW.config(fg='black')


#Displays red text if the box is clicked on, not filled out, then exited#
def on_PW_focusout(event):
    if PW.get() == '':
        PW.insert(0, "Enter your password")
        PW.config(fg="red")

def on_IP_click(event):
    if IP.get() == "Enter IP for commands":
        IP.delete(0, "end")  # delete all the text in the entry
        IP.insert(0, '')  # Insert blank for user input
        IP.config(fg='black')


#Displays red text if the box is clicked on, not filled out, then exited#
def on_IP_focusout(event):
    if IP.get() == '':
        IP.insert(0, "Enter IP for commands")
        IP.config(fg="green")

def on_INT_click(event):
    if INT.get() == "Enter Int/MAC/VLAN for commands":
        INT.delete(0, "end")  # delete all the text in the entry
        INT.insert(0, '')  # Insert blank for user input
        INT.config(fg='black')


#Displays red text if the box is clicked on, not filled out, then exited#
def on_INT_focusout(event):
    if INT.get() == '':
        INT.insert(0, "Enter Int/MAC/VLAN for commands")
        INT.config(fg="green")


#Sets up the "show active calls" button payload#
def active_click():
    try:
        # Sets up the SSH payload#
        cisco_payload = {
            "device_type": "cisco_ios",
            "host": device.get(),
            "username": UN.get(),
            "password": PW.get(),
            "port": 22,
        }

        # Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        # Commands section for this button#
        active = (ssh.send_command("sh call active voice comp"))

        # Binds the commands being run into a variable so that they can be printed neatly#
        results = (active)

        # Prints the output in the Python window for QA purposes#
        print(active)

        # Disconnects from the SSH session#
        ssh.disconnect()

        # Sets up the new window once the button is clicked#
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        # Creates the scrollbar#
        scroll = Scrollbar(output, orient=VERTICAL)
        scroll.grid(row=0, column=1, sticky="nsew")

        # Creates the section in the new window where the text will be printed then prints it#
        label = Label(output, text=results, relief=SUNKEN,  bg="black", fg="light green", justify=LEFT)
        label.grid(row=0, column=0, sticky=NS)

    # Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN, bg="black", fg="light green")
        label.grid(row=0, column=0, sticky=NSEW)


#Sets up the "show bgp summary" button payload#
def bgpsumm_click():
    try:
        # Sets up the SSH payload#
        cisco_payload = {
            "device_type": "cisco_ios",
            "host": device.get(),
            "username": UN.get(),
            "password": PW.get(),
            "port": 22,
        }

        # Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        # Commands section for this button#
        bgpsumm = (ssh.send_command("sh bgp summ | beg Neigh"))

        # Binds the commands being run into a variable so that they can be printed neatly#
        results = (bgpsumm)

        # Prints the output in the Python window for QA purposes#
        print(bgpsumm)

        # Disconnects from the SSH session#
        ssh.disconnect()

        # Sets up the new window once the button is clicked#
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        # Creates the scrollbar#
        scroll = Scrollbar(output, orient=VERTICAL)
        scroll.grid(row=0, column=1, sticky="nsew")

        # Creates the section in the new window where the text will be printed then prints it#
        label = Label(output, text=results, relief=SUNKEN,  bg="black", fg="light green", justify=LEFT)
        label.grid(row=0, column=0, sticky="nsew")

    #Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN, bg="black", fg="light green")
        label.grid(row=0, column=0, sticky="nsew")


#Sets up the "show cdp neighbors" button payload#
def  cdp_click():
    try:
        # Sets up the SSH payload#
        cisco_payload = {
            "device_type": "cisco_ios",
            "host": device.get(),
            "username": UN.get(),
            "password": PW.get(),
            "port": 22,
        }

        # Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        # Commands section for this button. Pulls any entries from the interface entry box#
        if INT.get() == "Enter interface for commands":
            cdp = (ssh.send_command("sh cdp neigh "))

        else:
            cd= ssh.send_command("sh cdp neigh " + INT.get())

        # Binds the commands being run into a variable so that they can be printed neatly#
        results = (cdp)

        # Prints the output in the Python window for QA purposes#
        print(cdp)

        # Disconnects from the SSH session#
        ssh.disconnect()

        # Sets up the new window once the button is clicked#
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        # Creates the scrollbar#
        scroll = Scrollbar(output, orient=VERTICAL)
        scroll.grid(row=0, column=1, sticky="nsew")

        # Creates the section in the new window where the text will be printed then prints it#
        label = Label(output, text=results, relief=SUNKEN,  bg="black", fg="#08bc08", justify=LEFT)
        label.grid(row=0, column=0)

    # Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN, bg="black", fg="light green")
        label.grid(row=0, column=0, sticky="nsew")


#Sets up the "show error-disabled" button payload#
def errdisable_click():
    try:
        # Sets up the SSH payload#
        cisco_payload = {
            "device_type": "cisco_ios",
            "host": device.get(),
            "username": UN.get(),
            "password": PW.get(),
            "port": 22,
        }

        # Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        # Commands section for this button#
        errdisabled = (ssh.send_command("sh int status | i err"))

        # Binds the commands being run into a variable so that they can be printed neatly#
        results = (errdisabled)

        # Prints the output in the Python window for QA purposes#
        print(errdisabled)

        # Disconnects from the SSH session#
        ssh.disconnect()

        # Sets up the new window once the button is clicked#
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        # Creates the scrollbar#
        scroll = Scrollbar(output, orient=VERTICAL)
        scroll.grid(row=0, column=1, sticky="nsew")

        # Creates the section in the new window where the text will be printed then prints it#
        label = Label(output, text=results, relief=SUNKEN,  bg="black", fg="light green", justify=LEFT)
        label.grid(row=0, column=0, sticky=NS)

    # Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN, bg="black", fg="light green")
        label.grid(row=0, column=0, sticky=NSEW)


#Sets up the "show interfaces" button payload#
def showint_click():

    try:
        #Sets up the SSH payload#
        cisco_payload = {
            "device_type": "cisco_ios",
            "host":device.get(),
            "username": UN.get(),
            "password": PW.get(),
            "port": 22,
        }

        #Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        #Commands section for this button#
        up = (ssh.send_command("sh ip int bri | i up"))
        down = (ssh.send_command("sh ip int bri | i down"))

        #Binds the commands being run into a variable so that they can be printed neatly#
        results= ("Up Interfacesnn" + up + "nnDown Interfacesnn" + down + "n")

        #Prints the output in the Python window for QA purposes#
        print("Up Interfacesnn" + up + "nnDown Interfacesnn" + down + "n")

        #Disconnects from the SSH session#
        ssh.disconnect()

        #Sets up the new window once the button is clicked#
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        #Creates the scrollbar#
        scroll = Scrollbar(output, orient=VERTICAL)
        scroll.grid(row=0, column=1, sticky="nsew")

        # Creates the section in the new window where the text will be printed then prints it#
        label = Label(output, text=results, relief=SUNKEN, bg="black", fg="light green", justify=LEFT)
        label.grid(row=0, column=0, sticky=NS)

    #Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN)
        label.grid(row=0, column=0, sticky=NSEW)


def iparp_click():

    try:
        #Sets up the SSH payload#
        cisco_payload = {
            "device_type": "cisco_ios",
            "host":device.get(),
            "username": UN.get(),
            "password": PW.get(),
            "port": 22,
        }

        #Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        #Commands section for this button#
        if IP.get() == "Enter IP for commands":
            iparp = (ssh.send_command("sh ip arp "))

        else:
            iparp = (ssh.send_command("sh ip arp " + IP.get()))

        #Binds the commands being run into a variable so that they can be printed neatly#
        results= (iparp)

        #Prints the output in the Python window for QA purposes#
        print(iparp)

        #Disconnects from the SSH session#
        ssh.disconnect()

        #Sets up the new window once the button is clicked#
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        #Creates the scrollbar#
        scroll = Scrollbar(output, orient=VERTICAL)
        scroll.grid(row=0, column=1, sticky="nsew")

        # Creates the section in the new window where the text will be printed then prints it#
        label = Label(output, text=results, relief=SUNKEN, bg="black", fg="light green", justify=LEFT)
        label.grid(row=0, column=0, sticky=NS)

    #Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN)
        label.grid(row=0, column=0, sticky=NSEW)


#Sets up the "show isdn status" button payload#
def isdn_click():
    try:
        # Sets up the SSH payload#
        cisco_payload = {
            "device_type": "cisco_ios",
            "host":device.get(),
            "username": UN.get(),
            "password": PW.get(),
            "port": 22,
        }

        # Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        # Commands section for this button#
        isdn = (ssh.send_command("sh isdn status"))

        # Binds the commands being run into a variable so that they can be printed neatly#
        results = (isdn)

        # Prints the output in the Python window for QA purposes#
        print(isdn)

        # Disconnects from the SSH session#
        ssh.disconnect()

        # Sets up the new window once the button is clicked#
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        # Creates the scrollbar#
        scroll = Scrollbar(output, orient=VERTICAL)
        scroll.grid(row=0, column=1, sticky="nsew")

        # Creates the section in the new window where the text will be printed then prints it#
        label = Label(output, text=results, relief=SUNKEN,  bg="black", fg="light green", justify=LEFT)
        label.grid(row=0, column=0, sticky=NS)

    #Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN, bg="black", fg="light green")
        label.grid(row=0, column=0, sticky="nsew")


#Sets up the "show mac address" button payload#
def mac_click():
    try:
        # Sets up the SSH payload#
        cisco_payload = {
            "device_type": "cisco_ios",
            "host": device.get(),
            "username": UN.get(),
            "password": PW.get(),
            "port": 22,
        }

        # Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        # Commands section for this button#
        if INT.get() == "Enter Int/MAC/VLAN for commands":
            mac = (ssh.send_command("show mac address-table"))

        else:
            mac = ssh.send_command("show mac address-table address " + INT.get())

        # Binds the commands being run into a variable so that they can be printed neatly#
        results = (mac)

        # Prints the output in the Python window for QA purposes#
        print(results)

        # Disconnects from the SSH session#
        ssh.disconnect()

        # Sets up the new window once the button is clicked#
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        # Creates the scrollbar#
        scroll = Scrollbar(output, orient=VERTICAL)
        scroll.grid(row=0, column=1, sticky="nsew")

        # Creates the section in the new window where the text will be printed then prints it#
        label = Label(output, text=results, relief=SUNKEN, bg="black", fg="#08bc08", justify=LEFT)
        label.grid(row=0, column=0)

    # Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN, bg="black", fg="light green")
        label.grid(row=0, column=0, sticky="nsew")


#Sets up the "show temperature" button payload#
def temp_click():
    try:
        # Sets up the SSH payload#
        cisco_payload = {
            "device_type": "cisco_ios",
            "host": device.get(),
            "username": UN.get(),
            "password": PW.get(),
            "port": 22,
        }

        # Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        # Commands section for this button#
        temp = (ssh.send_command("sh envi temp"))

        # Binds the commands being run into a variable so that they can be printed neatly#
        results = (temp)

        # Prints the output in the Python window for QA purposes#
        print(temp)

        # Disconnects from the SSH session#
        ssh.disconnect()

        # Sets up the new window once the button is clicked#
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        # Creates the scrollbar#
        scroll = Scrollbar(output, orient=VERTICAL)
        scroll.grid(row=0, column=1, sticky="ns")

        # Creates the section in the new window where the text will be printed then prints it#
        label = Label(output, text=results, relief=SUNKEN,  bg="black", fg="#08bc08", justify=LEFT)
        label.grid(row=0, column=0)

    # Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN, bg="black", fg="light green")
        label.grid(row=0, column=0, sticky="nsew")


#Sets up the "show uptime" button payload#
def uptime_click():
    try:
        # Sets up the SSH payload#
        cisco_payload = {
            "device_type": "cisco_ios",
            "host": device.get(),
            "username": UN.get(),
            "password": PW.get(),
            "port": 22,
        }

        # Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        # Commands section for this button#
        uptime = (ssh.send_command("sh ver | i uptime"))

        # Binds the commands being run into a variable so that they can be printed neatly#
        results = (uptime)

        # Prints the output in the Python window for QA purposes#
        print(uptime)

        # Disconnects from the SSH session#
        ssh.disconnect()

        # Sets up the new window once the button is clicked#
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        # Creates the scrollbar#
        scroll = Scrollbar(output, orient=VERTICAL)
        scroll.grid(row=0, column=1, sticky="ns")

        # Creates the section in the new window where the text will be printed then prints it#
        label = Label(output, text=results, relief=SUNKEN,  bg="black", fg="#08bc08", justify=LEFT)
        label.grid(row=0, column=0)

    # Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN, bg="black", fg="light green")
        label.grid(row=0, column=0, sticky="nsew")


#Sets up the "show vlan" button payload#
def vlan_click():
    try:
        # Sets up the SSH payload#
        cisco_payload = {
            "device_type": "cisco_ios",
            "host": device.get(),
            "username": UN.get(),
            "password": PW.get(),
            "port": 22,
        }

        # Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        # Commands section for this button#
        vlan = (ssh.send_command("sh vlan"))

        # Binds the commands being run into a variable so that they can be printed neatly#
        results = (vlan)

        # Prints the output in the Python window for QA purposes#
        print(vlan)

        # Disconnects from the SSH session#
        ssh.disconnect()

        # Sets up the new window once the button is clicked#
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        # Creates the scrollbar#
        scroll = Scrollbar(output, orient=VERTICAL)
        scroll.grid(row=0, column=1, sticky="nsew")

        # Creates the section in the new window where the text will be printed then prints it#
        label = Label(output, text=results, relief=SUNKEN, bg="black", fg="#08bc08", justify=LEFT)
        label.grid(row=0, column=0)

    # Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN, bg="black", fg="light green")
        label.grid(row=0, column=0, sticky="nsew")


#Sets up the command to exit the program on button click#
def quit_click():
    root.destroy()

#IP/Hostname entry field#
device = Entry(root, width=60, borderwidth=20, fg="black")
device.insert(0, "Enter the IP/Hostname you want to connect to")
device.bind("<FocusIn>", on_device_click)
device.bind("<FocusOut>", on_device_focusout)
device.grid(row=0, column=0, columnspan=2, sticky="nsew")

#Username entry field#
UN = Entry(root, width=60, borderwidth=20, fg="black")
UN.insert(0, "Enter your username")
UN.bind("<FocusIn>", on_UN_click)
UN.bind("<FocusOut>", on_UN_focusout)
UN.config(fg="gray")
UN.grid(row=1, column=0, columnspan=2, sticky="nsew")

#Password entry field#
PW = Entry(root, show="*", width=60, borderwidth=20, fg="black")
PW.insert(0, "*")
PW.bind("<FocusIn>", on_PW_click)
PW.bind("<FocusOut>", on_PW_focusout)
PW.config(fg="gray")
PW.grid(row=2, column=0, columnspan=2, sticky="nsew")

#IP for commands entry field#
IP = Entry(root, width=30, borderwidth=20, fg="black")
IP.insert(0, "Enter IP for commands")
IP.bind("<FocusIn>", on_IP_click)
IP.bind("<FocusOut>", on_IP_focusout)
IP.config(fg="gray")
IP.grid(row=3, column=0, sticky="nsew")

#Interface entry field#
INT = Entry(root, width=30, borderwidth=20, fg="black")
INT.insert(0, "Enter Int/MAC/VLAN for commands")
INT.bind("<FocusIn>", on_INT_click)
INT.bind("<FocusOut>", on_INT_focusout)
INT.config(fg="gray")
INT.grid(row=3, column=1, sticky="nsew")

#Creates the "show active calls" button#
active = Button(root, text="Show Active Calls", width=30, bg="#08bc08", pady=2, fg="black", borderwidth=10, command=active_click)
active.grid(row=4, column=0, sticky="nsew")

#Creates the "show bgp summary" button#
bgpsumm = Button(root, text="Show BGP Summary", width=30, bg="#08bc08", pady=2, fg="black", borderwidth=10, command=bgpsumm_click)
bgpsumm.grid(row=4, column=1, sticky="nsew")

#Creates the "show cdp neighbors" button#
cdp = Button(root, text="Show CDP neighbors", width=30, bg="#08bc08", pady=2, fg="black", borderwidth=10, command=cdp_click)
cdp.grid(row=5, column=0, sticky="nsew")

#Creates the "show error-disabled" button#
errdisable = Button(root, text="Show Err-Disabled Interfaces", width=30, bg="#08bc08", pady=2, fg="black", borderwidth=10, command=errdisable_click)
errdisable.grid(row=5, column=1, sticky="nsew")

#Creates the "show interfaces" button#
showint = Button(root, text="Show Interfaces", width=30, bg="#08bc08", pady=2, fg="black", borderwidth=10, command=showint_click)
showint.grid(row=6, column=0, sticky="nsew")

#"show ip arp" button"
isdn = Button(root, text="Show IP ARP", width=30, bg="#08bc08", pady=2, fg="black", borderwidth=10, command=iparp_click)
isdn.grid(row=6, column=1, sticky="nsew")

#"show isdn status" button"
isdn = Button(root, text="Show ISDN Status", width=30, bg="#08bc08", pady=2, fg="black", borderwidth=10, command=isdn_click)
isdn.grid(row=7, column=0, sticky="nsew")

#Creates the "show mac" button#
mac = Button(root, text="Show MAC Address", width=30, bg="#08bc08", pady=2, fg="black", borderwidth=10, command=mac_click)
mac.grid(row=7, column=1, sticky="nsew")

#Creates the "show temperature" button#
temp = Button(root, text="Show Temperature", width=30, bg="#08bc08", pady=2, fg="black", borderwidth=10, command=temp_click)
temp.grid(row=8, column=0, sticky="nsew")

#Creates the "show uptime" button#
uptime = Button(root, text="Show Uptime", width=30, bg="#08bc08", pady=2, fg="black", borderwidth=10, command=uptime_click)
uptime.grid(row=8, column=1, sticky="nsew")

#Creates the "show VLAN" button#
vlan = Button(root, text="Show VLAN", width=30, bg="#08bc08", pady=2, fg="black", borderwidth=10, command=vlan_click)
vlan.grid(row=9, column=0, sticky="nsew")

#Creates the "quit" button#
quit = Button(root, text="Quit", width=30, bg="red", fg="black", borderwidth=10, command=quit_click)
quit.grid(row=10, column=1, sticky="nsew")

#Row number buttons start at. Used for start of button auto re-size loop#
button_row_num = 4

#Creates button list#
button_list = [active, bgpsumm, cdp, errdisable, showint, isdn, temp, vlan, quit]

for button in button_list:
    Grid.rowconfigure(root, button_row_num, weight=1)
    Grid.columnconfigure(root, 0, weight=1)
    Grid.columnconfigure(root, 1, weight=1)
    button_row_num +=1

#Runs the app#
root.mainloop()

1 ответ
1

Подумайте о замене этого:

#Imported packages. You may need to run "pip install <package>" where <package> is the package listed below. If you are
#unsure how to install Python packages then try looking at instructions here:
# https://packaging.python.org/tutorials/installing-packages/#

с фактическим requirements.txt потребляемый pip install -rи объясните, каковы ваши требования к пакету. Вы даже можете оставить комментарий, показывающий, как передать файл в pip; но ваш комментарий в его нынешнем виде менее чем полезен, потому что на самом деле он не говорит о ваших требованиях.

Попробуйте удалить root из глобального пространства имен, чтобы ваш код можно было повторно использовать. Наиболее типичный подход к этому — поместить его в класс. Точно так же ничего после #IP/Hostname entry field# следует оставить глобальным.

Ваша программа поддерживает ввод пароля (буквально) Enter your password? Я подозреваю, что нет. Вы могли бы по-настоящему представить себя и включить отслеживание изменений, чтобы знать, когда кто-то ввел свой собственный текст, но проще и менее удивительно удалить текст-заполнитель из текстового поля и поместить его в отдельную метку.

Нет смысла формировать словарь cisco_payload, поскольку вы используете его только для одного набора кваргов. Вместо этого просто

ssh = ConnectHandler(
    device_type="cisco_ios",
    host=device.get(),
    username=UN.get(),
    password=PW.get(),
    port=22,
)

Также обратите внимание, что UN а также PW не должны быть ни заглавными, ни сокращенными.

Ни то, ни другое:

    active = (ssh.send_command("sh call active voice comp"))
    results = (active)

следует заключить в круглые скобки.

Этот блок:

        # Sets the SSH payload to a variable to be used for running commands#
        ssh = ConnectHandler(**cisco_payload)

        # Commands section for this button#
        active = (ssh.send_command("sh call active voice comp"))

        # Binds the commands being run into a variable so that they can be printed neatly#
        results = (active)

        # Prints the output in the Python window for QA purposes#
        print(active)

        # Disconnects from the SSH session#
        ssh.disconnect()

должен гарантировать отключение:

# Sets the SSH payload to a variable to be used for running commands#
ssh = ConnectHandler(**cisco_payload)
        
try:
    # Commands section for this button#
    active = (ssh.send_command("sh call active voice comp"))

    # Binds the commands being run into a variable so that they can be printed neatly#
    results = (active)

    # Prints the output in the Python window for QA purposes#
    print(active)

finally:
    # Disconnects from the SSH session#
    ssh.disconnect()

Вам следует отказаться от привычки добавлять # суффикс ко всем вашим комментариям.

В ваших функциях много повторений, например, в вашей настройке окна вывода верхнего уровня, такого как это:

    # Handles error exceptions and prints out the error#
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN, bg="black", fg="light green")
        label.grid(row=0, column=0, sticky="nsew")

Если вы хотите создать универсальный обработчик, который будет выполнять вышеуказанные действия для любых вызывающих абонентов, создайте диспетчер контекста:

@contextlib.contextmanager
def handle_and_output():
    try:
        yield
    except(KeyError, KeyboardInterrupt, IndexError, NetmikoTimeoutException, NetmikoAuthenticationException) as error:
        output = Toplevel(root)
        output.title("Output Window")
        output.iconbitmap('cisco.ico')

        label = Label(output, text=error, relief=SUNKEN, bg="black", fg="light green")
        label.grid(row=0, column=0, sticky="nsew")

# ...

with handle_and_output():
    # do something risky

Однако повторение создания окна верхнего уровня кажется подозрительным, и, вероятно, должен быть только один экземпляр, ссылку на который вы повторно используете, устанавливая его текст метки.

  • 1

    +1 Самая большая проблема с этим кодом в том, что он излишне длинный. Повторяющийся код плох, потому что его труднее читать (длиннее и менее значимо), сложнее изменить (нужно обновить несколько мест) и более подвержен ошибкам (например, легко обновить одно место и забыть о других). Общий принцип программирования: не повторяйся. Разумная цель — не иметь повторяющихся или почти повторяющихся блоков из 2+ строк, может быть, даже одной строки.

    — Захари Вэнс



  • 1

    Спасибо вам обоим за совет. Я просматриваю сценарий и пытаюсь внести каждое из предложенных изменений. Я еще не достаточно умен, чтобы вызывать функции в функциях, но буду работать над тем, чтобы это произошло. Я согласен, что код излишне длинный и его нужно урезать. В то время для меня имело смысл копировать и корректировать то, что у меня уже работало, поскольку я был единственным, кто что-либо изменял. Этому сценарию предстоит пройти долгий путь, и вы оба дали мне необходимые советы, чтобы сделать его лучше.

    — Гирлиман

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *