Python 3, Tkinter GUI, Juniper Switch DevOps task

153 views Asked by At

I've been doing python for around 3 weeks now so very new to it. Don't judge me on my code! :)

I have made an app to enable basic network ops functions to be performed at the click of a button, tasks like: Disable/ Enable interface Disable/ Enable interface security Show interface Vlan/Status Database of switches with auto lookup based on computer name.

However, the last thing I need is to be able to inspect a log file on the switch and then every few seconds run an update in order to get the last few entries. This is used to find a user by monitoring an interface for up/down status when they pull their connection and then plug it back in again.

I have got the code to extract the file cut separate it into a list then edit the list to show the last 10 entries: This uses the Junos-EZNC library to interact with the switch:

def monitorInterface():        
    username = usernameEntry.get()      # Get username from entrybox in mainWindow
    password1 = passwordEntry.get()     # Get password from entrybox in mainWindow
    hostIP = SwitchIPEntry.get()        # Get switch IP from entrybox in mainWindow
    switchPort = SwitchPortEntry.get()  # Get switchport from entrybox in mainWindow
    count = 0
    with Device(host=hostIP, user=username, password=password1) as dev: # Logon bits
        dev.open() # Open connection

        while count < 10: 
            logReturn = dev.rpc.get_log(filename='messages') # Grab file 
            logReturn1 = etree.tostring(logReturn, encoding='unicode', pretty_print=True) # convert file to readable
            logReturn2 = logReturn1.split('\n') # Split file in to list based on new line
            logReturn3 = logReturn2[-10:] # read last 10 results of the list
            TextBoxData.insert(INSERT, logReturn3) # Print List of 10 in textbox
        dev.close()

The problem I am having is that I want this def to toggle on/off and have it run every few seconds so that it can show the up/down message that appears on the log that is being pulled off the switch in almost real-time give or take a few seconds.

At the moment my code locks up during the def running. What I'm looking for is some sort of subprocesses? I can push this to so that the main loop is not affected by its running? So I can have "while button x is true: (do the function) and not stop the GUI from working.

I'm at the end of my skill/knowledge and I cant seem to see a good answer on Google! maybe I just don't know what to call the fix!

Any help would be great!

Ryan

1

There are 1 answers

0
Ryan On BEST ANSWER

AMC,

Thank you for pointing me in the right direction! Threading was the bit I was missing!

So after a bit more Google-ing and head scratching I found the right example.

import time
import threading

def threadingFunc():
    t1 = threading.Thread(target=theFunctionToRun)
    t1.start()

def theFunchtionToRun():
    #do stuff here#

checkbox that calls the function threadingFunc

And that did the trick, Now my GUI stays alive and my "monitor interface-logs" equivalent works like a charm!

Thank you!

Ryan