我是编程新手,现在正在为我的网络工程师工作做一个小项目。
这个应用程序的整个想法:输入 IP、MAC、用户名、密码:并查看来自以下功能的打印:
登录到设备! 收集信息 等
并在print_out = tkinter.Text框中实时显示所有这些信息
目前,我有 2 个 .py 文件: MAC_search_GUI.py MAC_search.py
MAC_search_GUI.py是一个tkinter 窗口,我可以在其中输入IP、MAC、用户名和密码+ 还有另一个窗口可以查看日志:
import tkinter
from tkinter import Button, Text
from tkinter import simpledialog
from MAC_search import MAC_search_func
def show_entry_fields():
IP = e1.get()
MAC = e2.get()
username = e3.get()
password = e4.get()
"""
e1.delete(0, tkinter.END)
e2.delete(0, tkinter.END)
e3.delete(0, tkinter.END)
e4.delete(0, tkinter.END)
"""
SESSION = {'ip': IP,
'device_type':'cisco_ios',
'username': username,
'password': password}
result = MAC_search_func(IP, MAC, **SESSION)
return print(result)
# f4b5.2fa0.8fca
root = tkinter.Tk()
tkinter.Label(root, text="IP:").grid(row=1)
tkinter.Label(root, text="MAC:").grid(row=2)
tkinter.Label(root, text="Username").grid(row=3)
tkinter.Label(root, text="Password").grid(row=4)
e1 = tkinter.Entry(root)
e2 = tkinter.Entry(root)
e3 = tkinter.Entry(root)
e4 = tkinter.Entry(root)
e1.grid(row=1, column=1)
e2.grid(row=2, column=1)
e3.grid(row=3, column=1)
e4.grid(row=4, column=1)
print_out = tkinter.Text(root, height = 20, width = 60, bg = "light cyan").grid(row=7, column=2)
tkinter.Button(root, text='Apply', command=show_entry_fields).grid(row=5, column=1, sticky=tkinter.W,
pady=4)
root.geometry("600x600")
root.mainloop()
MAC_search_func函数位于MAC_search.py 中,连接到输入 IP 并收集有关该设备的信息:
import netmiko
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
from netmiko.ssh_exception import NetMikoAuthenticationException
from paramiko.ssh_exception import SSHException
def MAC_search_func(arg_ip, arg_mac, **arg_session):
def conv_Po_to_Int(arg_int):
JSON = connection.send_command("show etherchannel summary", use_textfsm=True)
for line in JSON:
if line['po_name'] == arg_int:
int_status = line['interfaces']
return int_status[0]
interface = ''
try:
connection = netmiko.ConnectHandler(**arg_session)
print("Connection is succefull")
except NetMikoTimeoutException:
return print(" #### Device is not reachable! ####")
except NetMikoAuthenticationException:
return print(" #### Wrong Username or Password! ####")
except SSHException:
return print(" #### Make sure SSH is enabled! ####")
# Looking for MAC in Mac address table
JSON = connection.send_command("show mac address-table", use_textfsm=True)
for line in JSON:
if line['destination_address'] == arg_mac:
interface = line['destination_port']
# Checking if interface is Port channel
if interface[0:2] == "Po":
interface = conv_Po_to_Int(interface)
# IF MAC is not found
if interface == '':
return print("This MAC-ADDRESS IS NOT FOUND ON THIS IP: "+ arg_ip)
# If Mac was found on switch checking if Int is Trunk or Access
JSON = connection.send_command("show interfaces status", use_textfsm=True)
for line in JSON:
if line['port'] == interface:
int_status = line['vlan']
# if port is trunk checking which device located on another end
if int_status == "trunk":
JSON = connection.send_command("show cdp neighbors " + interface, use_textfsm=True)
for line in JSON:
next_switch = line['neighbor']
JSON = connection.send_command("show cdp entry " + next_switch)
result = JSON.find('IP address:')
return print("Looks like this mac located on device with " + JSON[result:(result + 21)] + " and hostname: " + next_switch)
else:
return print("MAC was found on " + interface)
connection.disconnect()
所以,我有几个问题:
我不知道如何将RETURN从我的MAC_search_GUI函数发送回MAC_search_GUI,所以我可以打印它:
print_out = tkinter.Text(root, height = 20, width = 60, bg = "light cyan").grid(row=7, column=2)
当我按下Apply时,应用程序没有响应,直到所有功能都不会完成(5-10 秒),因为连接和从交换机获取信息需要时间。如何在我单击Apply后做到这一点,应用程序不会等到结束,而是继续工作,直到Return不会出现在print_out窗口中