Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. import subprocess, time, re, math, threading, queue
  2. import PySimpleGUI as sg
  3. from sys import platform
  4.  
  5. STEP_SIZE = 1
  6. NO_OF_PINGS = 500
  7. MAX_PING = 250
  8. CANVAS_SIZE = (800, 600)
  9. ping = ""
  10.  
  11. class justAList():
  12. def __init__(self):
  13. self.matchList = ['', '', '']
  14.  
  15.  
  16. def main_loop(matchClass, gui_queue):
  17. sg.change_look_and_feel('Dark Blue 3')
  18. inputLayout = [
  19. [sg.Text('Enter domain name of what you want to ping')],
  20. [sg.InputText(size=(37, 1), key='dn')],
  21. [sg.Submit(), sg.Cancel()]
  22. ]
  23.  
  24. inputWindow = sg.Window('Domain name entry', inputLayout)
  25. event, value = inputWindow.read()
  26. if event is None or event == 'Cancel':
  27. return
  28.  
  29. matchClass.matchList[0] = value['dn']
  30. if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
  31. 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()))
  32. elif platform == 'win32':
  33. 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()))
  34.  
  35. inputWindow.close()
  36. del inputWindow
  37.  
  38. sg.set_options(element_padding=(0,0))
  39.  
  40. sg.change_look_and_feel('Dark Blue 3')
  41. graphLayout = [
  42. [sg.Text("The IP address of hostname '" + matchClass.matchList[0] + "' is: " + matchClass.matchList[1], size=(100, 1), )],
  43. [sg.Graph(CANVAS_SIZE, (0, 0), (NO_OF_PINGS, MAX_PING),
  44. background_color='white', key='graph')],
  45. [sg.Button('Exit', button_color=('black', 'white'))]
  46. ]
  47.  
  48. graphWindow = sg.Window(matchClass.matchList[0], graphLayout, grab_anywhere=True, no_titlebar=False, use_default_focus=False, finalize=True)
  49. graph = graphWindow['graph']
  50.  
  51. threading.Thread(target=ping, args=(10, gui_queue, matchClass), daemon=True).start()
  52.  
  53. i, j = 0, 1
  54. prev_x, prev_y = 0, 0
  55. graph_value = 0
  56.  
  57. while True:
  58. graph_value = 0
  59. event, values = graphWindow.read(timeout=25)
  60. if event == 'Exit' or event is None:
  61. break
  62.  
  63. while True:
  64. try:
  65. graph_value = gui_queue.get_nowait()
  66. except queue.Empty:
  67. break
  68.  
  69. if graph_value > 0:
  70. print("Iteration #" + str(j))
  71.  
  72. if graph_value > MAX_PING:
  73. graph_value = MAX_PING
  74. if graph_value < 0:
  75. graph_value = 0
  76.  
  77. new_x, new_y = i, graph_value
  78.  
  79. if i >= NO_OF_PINGS:
  80. graph.move(-STEP_SIZE, 0)
  81. prev_x = prev_x - STEP_SIZE
  82.  
  83. graph.draw_line((prev_x, prev_y), (new_x, new_y), color='red')
  84. prev_x, prev_y = new_x, new_y
  85. i += STEP_SIZE if i < NO_OF_PINGS else 0
  86. j += 1
  87.  
  88. graphWindow.close()
  89. del graphWindow
  90.  
  91. def ping(run_freq, gui_queue, matchClass):
  92. while True:
  93. try:
  94. time.sleep(run_freq/1000)
  95. ping = ""
  96. if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
  97. ping = subprocess.run(['ping', '-c', '1', matchClass.matchList[0]], capture_output=True, text=True)
  98. elif platform == 'win32':
  99. ping = subprocess.run(['ping', '-n', '1', matchClass.matchList[0]], capture_output=True, text=True)
  100. cmdOutput = ''.join(re.findall('time=(\d+)', ping.stdout))
  101. if cmdOutput.isdigit() == True:
  102. gui_queue.put(int(cmdOutput))
  103. except ValueError:
  104. continue
  105.  
  106.  
  107. if __name__ == '__main__':
  108. matchClass = justAList()
  109.  
  110. gui_queue = queue.Queue()
  111.  
  112. main_loop(matchClass, gui_queue)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement