Skip to content

Instantly share code, notes, and snippets.

@hirman74
Created November 5, 2022 10:28
Show Gist options
  • Save hirman74/635064709e2be32d36269514933bd555 to your computer and use it in GitHub Desktop.
Save hirman74/635064709e2be32d36269514933bd555 to your computer and use it in GitHub Desktop.
From DO_PyEZ_Cookbook.pdf successful tkinter
'''
C:\Python389\python.exe -m pip install --upgrade pip
C:\Python389\Scripts\pip3.8.exe install virtualenv
C:\Python389\python.exe -m virtualenv -p="C:\Python389\python.exe" virtenv
Activate.bat
C:\Python389\Scripts\pip3.8.exe install junos-eznc
C:\Python389\python.exe
From https://www.juniper.net/documentation/en_US/day-one-books/DO_PyEZ_Cookbook.pdf
Recipe 14 - Adding a Graphical Interface to the PyEZ Script by Peter Klimai
tkinter buttons not fully added
'''
from tkinter import *
from jnpr.junos import Device
from jnpr.junos.exception import *
from pprint import pformat
from lxml import etree
USER = 'lab'
PASSWD = 'lab123'
DEVICE_IP = '10.254.0.35'
def output(st):
text.insert(END, chars=st)
text.see(END)
def read_and_display(message, function):
output(message)
try:
with Device(host=entry_dev.get(), user=entry_user.get(), password=entry_pw.get()) as dev:
res = function(dev)
except ConnectRefusedError:
print('\nError: Connection refused!\n')
except ConnectTimeoutError:
output('\nConnection timeout error!\n')
except ConnectUnknownHostError:
output('\nError: Connection attempt to unknown host.\n')
except ConnectionError:
output('\nConnection error!\n')
except ConnectAuthError:
output('\nConnection authentication error!\n')
else:
output(res)
def print_facts():
read_and_display('\nDevice facts:\n', lambda dev: pformat(dev.facts))
def show_bgp(): # (6)
read_and_display('\nBGP summary information:', lambda dev: dev.rpc.get_bgp_summary_information({'format': 'text'}).text)
def show_intf(): # (7)
read_and_display('\nInterface information:', lambda dev: dev.rpc.get_interface_information({'format': 'text'}, terse=True, normalize=True).text)
def show_route():
read_and_display('\nManually type commands:', lambda dev: dev.display_xml_rpc('show route', format='text').text)
def show_software_information():
#invoke the RPC equivalent to "show version"
read_and_display('\nSoftware information:', lambda dev: etree.tostring(dev.rpc.get_software_information({'format':'text'}), encoding='unicode'))
def show_route():
read_and_display('\nManually type commands:', lambda dev: dev.rpc.get_route_information(table='inet.0', dev_timeout=55).text)
def main(): # (8)
global entry_dev, entry_user, entry_pw, text
root = Tk() # (9)
Frame(root, height=10).grid(row=0) # (10)
Label(root, text='Device address:').grid(row=1, column=0) # (11)
entry_dev = Entry(root) # (12)
entry_dev.grid(row=1, column=1)
entry_dev.insert(END, DEVICE_IP)
Label(root, text='Login:').grid(row=2, column=0) # (13)
entry_user = Entry(root)
entry_user.grid(row=2, column=1)
entry_user.insert(END, USER)
Label(root, text='Password:').grid(row=3, column=0) # (14)
entry_pw = Entry(root, show='*')
entry_pw.grid(row=3, column=1)
entry_pw.insert(END, PASSWD)
Frame(root, height=10).grid(row=4) # (15)
Button(root, text='Read facts!', command=print_facts).grid(row=5, column=0)
Button(root, text='Show interfaces!', command=show_intf).grid(row=5, column=1)
Button(root, text='Show BGP!', command=show_bgp).grid(row=5, column=2)
Frame(root, height=10).grid(row=6)
frame = Frame(root, width=800, height=700) # (16)
frame.grid(row=7, column=0, columnspan=4)
frame.grid_propagate(False)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
text = Text(frame, borderwidth=3)
text.config(font=('courier', 11), wrap='none')
text.grid(row=0, column=0, sticky='nsew', padx=2, pady=2)
scrollbarY = Scrollbar(frame, command=text.yview) # (17)
scrollbarY.grid(row=0, column=1, sticky='nsew')
text['yscrollcommand'] = scrollbarY.set
scrollbarX = Scrollbar(frame, orient=HORIZONTAL, command=text.xview)
scrollbarX.grid(row=1, column=0, sticky='nsew')
text['xscrollcommand'] = scrollbarX.set
root.mainloop() # (18)
if __name__ == '__main__': # (19)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment