问题描述
我是编程新手,现在正在为我的网络工程师工作做一个小项目。
此应用程序的整体思想:输入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()
因此,我对此有一些疑问:
-
我不知道如何将我的 MAC_searc h函数中的 RETURN 发送回 MAC_search_GUI ,因此可以打印它与:
print_out = tkinter.Text(root,column=2)
-
当我按 Apply 时,该应用程序没有响应,直到所有功能都无法完成(5-10秒),因为连接和从交换机获取信息需要时间。在我点击应用后,应用将如何运行,直到等到返回不会显示在 print_out 窗口
解决方法
抱歉,桅杆
解决方案是:
-
第一个问题:
result = MAC_search_func(IP,MAC,**SESSION) return print_out.insert("end-1c",result)
-
对于我的第二个问题,我没有找到完整的解决方案,但是现在它的运行速度更快。