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)
- ping = ""
- class justAList():
- def __init__(self):
- self.matchList = ['', '', '']
- def main_loop(matchClass, gui_queue):
- lowestPing = averagePing = highestPing = totalPing = 0
- sg.change_look_and_feel('Dark Blue 3')
- inputLayout = [
- [sg.Text('Enter domain name of what you want to ping')],
- [sg.InputText(size=(37, 1), key='dn')],
- [sg.Submit(), sg.Cancel()]
- ]
- inputWindow = sg.Window('Domain name entry', inputLayout)
- event, value = inputWindow.read()
- if event is None or event == 'Cancel':
- return
- matchClass.matchList[0] = value['dn']
- if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
- matchClass.matchList[1] = ''.join(re.findall('Ping statistics for (\d+.\d+.\d+.\d+)', subprocess.run(['ping', '-c', '1', matchClass.matchList[0]], capture_output=True).stdout.decode()))
- elif platform == 'win32':
- matchClass.matchList[1] = ''.join(re.findall('Ping statistics for (\d+.\d+.\d+.\d+)', subprocess.run(['ping', '-n', '1', matchClass.matchList[0]], capture_output=True).stdout.decode()))
- inputWindow.close()
- del inputWindow
- sg.set_options(element_padding=(0,0))
- sg.change_look_and_feel('Dark Blue 3')
- graphLayout = [
- [sg.Text(" The IP address of hostname '" + matchClass.matchList[0] + "' is: " + matchClass.matchList[1], size=(100, 1), )],
- [sg.Text("")],
- [sg.Text("Ping\n(ms)", size=(5, 2)), sg.Graph((20, 600), (0, 0), (20, MAX_PING), background_color='white', key='yAxis'),
- sg.Graph(CANVAS_SIZE, (0, 0), (NO_OF_PINGS, MAX_PING), background_color='white', key='mainGraph')],
- [sg.Text(size=(5, 1)), sg.Graph((822, 20), (0, 0), (NO_OF_PINGS, 20), background_color='white', key='xAxis')],
- [sg.Text(" Number of pings: 0\t\t", key='nP'), sg.Text("Lowest ping: " + str(lowestPing) + '\t\t', key='lP'),
- sg.Text("Average ping: " + str(averagePing) + '\t\t', key='aP'), sg.Text("Highest ping: " + str(highestPing), key='hP')],
- [sg.Button('Exit', button_color=('black', 'white'))]
- ]
- graphWindow = sg.Window(matchClass.matchList[0], graphLayout, grab_anywhere=True, no_titlebar=False, use_default_focus=False, finalize=True)
- mainGraph = graphWindow['mainGraph']
- xGraph = graphWindow['xAxis']
- yGraph = graphWindow['yAxis']
- yGraph.DrawLine((20, 0), (20, MAX_PING))
- xGraph.DrawLine((13, 19), (NO_OF_PINGS, 19))
- for x in range(20, NO_OF_PINGS - 1, 20):
- xGraph.DrawLine((x, 10), (x, 20))
- if x != 0:
- xGraph.DrawText(x, (x, 5), color='green')
- for y in range(20, MAX_PING - 1, 20):
- yGraph.DrawLine((10, y), (20, y))
- if y != 0:
- yGraph.DrawText(y, (10, y-3), color='blue')
- threading.Thread(target=ping, args=(10, gui_queue, matchClass), daemon=True).start()
- i, j = 0, 1
- prev_x, prev_y = 0, 0
- graph_value = 0
- while True:
- graph_value = 0
- event, values = graphWindow.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:
- if j == 1:
- lowestPing = averagePing = highestPing = graph_value
- else:
- if lowestPing > graph_value:
- lowestPing = graph_value
- if highestPing < graph_value:
- highestPing = graph_value
- totalPing += graph_value
- averagePing = '{:.2f}'.format(totalPing / j, 2)
- graphWindow['nP'].Update(" Number of pings: " + str(j) + '\t\t')
- graphWindow['lP'].Update("Lowest ping: " + str(lowestPing) + '\t\t')
- graphWindow['aP'].Update("Average ping: " + averagePing + '\t')
- graphWindow['hP'].Update("Highest ping: " + str(highestPing))
- #print("Iteration #" + str(j) + " - highest ping is " + str(highestPing))
- 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:
- mainGraph.move(-STEP_SIZE, 0)
- prev_x = prev_x - STEP_SIZE
- mainGraph.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
- graphWindow.close()
- del graphWindow
- def ping(run_freq, gui_queue, matchClass):
- while True:
- try:
- time.sleep(run_freq/1000)
- ping = ""
- if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
- ping = subprocess.run(['ping', '-c', '1', matchClass.matchList[0]], capture_output=True, text=True)
- elif platform == 'win32':
- ping = subprocess.run(['ping', '-n', '1', matchClass.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__':
- matchClass = justAList()
- gui_queue = queue.Queue()
- main_loop(matchClass, gui_queue)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement