如何从另一个 py 页面调用 netmiko ConnectHandler 并仅使用选择中的按钮执行它

how to call netmiko ConnectHandler from another py page and execute it only with button in choose

我需要能够从 scripter.py 调用脚本,方法是从下拉列表中选择它并单击一个按钮 运行 它并在相同的 window。所以我有 2 个主要问题,第一个是如何从另一个 py 页面调用 netmiko ConnectHandler 并仅使用选择中的按钮执行它问题是当我 运行 main.py 它执行 scripter.py之后 tkinter 自动打开

第二个是不知道如何将输出从 scripter.py 导出到 main.py 并在 tkinter

中显示

所以我的 (scripter.py) 看起来像

from netmiko import ConnectHandler
    
fortinet_device = {
    'device_type': 'fortinet',
    'ip': 'X.X.X.X',
    'username': 'Username',
    'password': 'Password'
}
    
net_connect = ConnectHandler(**fortinet_device)
    
cmd = ['get sys arp']
output = ''
for command in cmd:
    output += net_connect.send_command(command)
    
print(output)

我的主页是 (main.py)

from tkinter import *
from scripter import cmd

master = Tk(className="Test")
master.geometry("300x300")

def func(selected_item):
    print(repr(selected_item.strip()))

#************main_menu menu***********
main_menu = StringVar(master)
    
#***********dependent_menu menu*********
dependent_menu = StringVar(master)

#************main_menu values**************
show_arp = cmd 
show_add = 'show firewall address'

#***********dependent_menu values************
grep = '|grep'

# main_menu default value
main_menu.set(show_arp)

# dependent_menu default value
dependent_menu.set(grep)

#************main_menu**************
w = OptionMenu(master, main_menu, show_arp, show_add, "three")
w.pack(side=TOP)

#**************dependent_menu***************
s = OptionMenu(master, dependent_menu, grep)
s.pack(side=LEFT)

mainloop()

首先你需要把scripter.py里面的代码放在一个函数里面,例如:

from netmiko import ConnectHandler

def get_sys_arp():
    fortinet_device = {
        'device_type': 'fortinet',
        'ip': 'X.X.X.X',
        'username': 'username',
        'password': 'password'
    }
        
    net_connect = ConnectHandler(**fortinet_device)
        
    cmd = ['get sys arp']
    output = ''
    for command in cmd:
        output += net_connect.send_command(command)
        
    print(output)

然后在main.py里面导入这个函数,当OptionMenu里面对应的item被选中时执行:

from tkinter import *
from scripter import get_sys_arp   # import the function from scripter

master = Tk(className="Test")
master.geometry("300x300")

def func(selected_item):
    print(repr(selected_item.strip()))
    if selected_item == 'show arp':
        get_sys_arp()  # execute the function

#************main_menu menu***********
main_menu = StringVar(master)

#***********dependent_menu menu*********
dependent_menu = StringVar(master)

#************main_menu values**************
show_arp = 'show arp'   # item text for running get_sys_arp()
show_add = 'show firewall address'

#***********dependent_menu values************
grep = '|grep'

# main_menu default value
main_menu.set(show_arp)

# dependent_menu default value
dependent_menu.set(grep)

#************main_menu**************
# add command=func to execute func() when a item is selected
w = OptionMenu(master, main_menu, show_arp, show_add, "three", command=func)
w.pack(side=TOP)

#**************dependent_menu***************
s = OptionMenu(master, dependent_menu, grep)
s.pack(side=LEFT)

mainloop()