Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess, time, re, math, threading, queue
- import PySimpleGUI as sg
- from sys import platform
- STEP_SIZE = 1
- NO_OF_PINGS = 500
- MAX_PING = 250
- CANVAS_SIZE = (800, 600)
- matchList = ['', '', '']
- ping = ""
- def main_loop(gui_queue):
- global matchList, ping
- sg.change_look_and_feel('Dark Blue 3')
- layout = [
- [sg.Text('Enter domain name of what you want to ping')],
- [sg.Input(size=(37, 1), key='dn')],
- [sg.Ok(), sg.Cancel()]
- ]
- window = sg.Window('Domain name entry', layout)
- event, value = window.read()
- if event is None or event == 'Cancel':
- return
- matchList[0] = value['dn']
- if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
- matchList[1] = ''.join(re.findall('Ping statistics for (\d+.\d+.\d+.\d+)', subprocess.run(['ping', '-c', '1', matchList[0]], capture_output=True).stdout.decode()))
- elif platform == 'win32':
- matchList[1] = ''.join(re.findall('Ping statistics for (\d+.\d+.\d+.\d+)', subprocess.run(['ping', '-n', '1', matchList[0]], capture_output=True).stdout.decode()))
- window.close()
- del window
- sg.set_options(element_padding=(0,0))
- sg.change_look_and_feel('Dark Blue 3')
- layout = [
- [sg.Text("The IP address of hostname '" + matchList[0] + "' is: " + matchList[1], size=(100, 1), )],
- [sg.Graph(CANVAS_SIZE, (0, 0), (NO_OF_PINGS, MAX_PING),
- background_color='white', key='graph')],
- [sg.Button('Exit', button_color=('black', 'white'))]
- ]
- window = sg.Window(matchList[0], layout, grab_anywhere=True, no_titlebar=False, use_default_focus=False, finalize=True)
- graph = window['graph']
- threading.Thread(target=ping, args=(10, gui_queue), daemon=True).start()
- i, j = 0, 1
- prev_x, prev_y = 0, 0
- graph_value = 0
- while True:
- graph_value = 0
- event, values = window.read(timeout=25)
- if event == 'Exit' or event is None:
- break
- while True:
- try:
- graph_value = gui_queue.get_nowait()
- except queue.Empty:
- break
- if graph_value > 0:
- print("Iteration #" + str(j))
- if graph_value > MAX_PING:
- graph_value = MAX_PING
- if graph_value < 0:
- graph_value = 0
- new_x, new_y = i, graph_value
- if i >= NO_OF_PINGS:
- graph.move(-STEP_SIZE, 0)
- prev_x = prev_x - STEP_SIZE
- graph.draw_line((prev_x, prev_y), (new_x, new_y), color='red')
- prev_x, prev_y = new_x, new_y
- i += STEP_SIZE if i < NO_OF_PINGS else 0
- j += 1
- window.close()
- def ping(run_freq, gui_queue):
- while True:
- try:
- time.sleep(run_freq/1000)
- ping = ""
- if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
- ping = subprocess.run(['ping', '-c', '1', matchList[0]], capture_output=True, text=True)
- elif platform == 'win32':
- ping = subprocess.run(['ping', '-n', '1', matchList[0]], capture_output=True, text=True)
- cmdOutput = ''.join(re.findall('time=(\d+)', ping.stdout))
- if cmdOutput.isdigit() == True:
- gui_queue.put(int(cmdOutput))
- except ValueError:
- continue
- if __name__ == '__main__':
- gui_queue = queue.Queue()
- main_loop(gui_queue)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement