I need to be able to call the script from the scripter.py by choosing it from the drop-down list and clicking a button to run it and get the output in the same window. so I have 2 main problems the first is how to call netmiko ConnectHandler from another py page and execute it only with button in choose the problem is when I run the main.py it executes the scripter.py automatically after that the tkinter is opens
the second is a don't know how to export the output from scripter.py to the main.py and show it in tkinter
So my (scripter.py) looks like
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)
And my main page is (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()
First you need to put the code inside scripter.py
inside a function, for example:
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)
Then import this function inside main.py
and execute it when the corresponding item in OptionMenu
is selected:
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()