Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. import subprocess, time, re, math
  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. matchList = ['', '', '']
  10.  
  11. def main_loop():
  12. global matchList
  13. sg.change_look_and_feel('Dark Blue 3')
  14. layout = [
  15. [sg.Text('Enter domain name of what you want to ping')],
  16. [sg.Input(size=(37, 1), key='dn')],
  17. [sg.Ok(), sg.Cancel()]
  18. ]
  19.  
  20. window = sg.Window('Domain name entry', layout)
  21. event, value = window.read()
  22. if event is None or event == 'Cancel':
  23. return
  24.  
  25. matchList[0] = value['dn']
  26. if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
  27. matchList[1] = ''.join(re.findall('Ping statistics for (\d+.\d+.\d+.\d+)', subprocess.run(['ping', '-c', '1', matchList[0]], capture_output=True).stdout.decode()))
  28. elif platform == 'win32':
  29. matchList[1] = ''.join(re.findall('Ping statistics for (\d+.\d+.\d+.\d+)', subprocess.run(['ping', '-n', '1', matchList[0]], capture_output=True).stdout.decode()))
  30.  
  31. window.close()
  32.  
  33. sg.set_options(element_padding=(0,0))
  34.  
  35. sg.change_look_and_feel('Dark Blue 3')
  36. layout = [
  37. [sg.Text("The IP address of hostname '" + matchList[0] + "' is: " + matchList[1], size=(100, 1), )],
  38. [sg.Graph(CANVAS_SIZE, (0, 0), (NO_OF_PINGS, MAX_PING),
  39. background_color='white', key='graph')],
  40. [sg.Button('Exit', button_color=('black', 'white'))]
  41. ]
  42.  
  43. window = sg.Window(matchList[0], layout, grab_anywhere=True, no_titlebar=False, use_default_focus=False, finalize=True)
  44. graph = window['graph']
  45.  
  46. i = 0
  47. prev_x, prev_y = 0, 0
  48. graph_value = 0
  49. while True:
  50. event, values = window.read(timeout=10)
  51. if event == 'Exit' or event is None:
  52. break
  53.  
  54. #time.sleep(0.01)
  55. #print(str(i) + " before ping")
  56. #ping = subprocess.run(['ping', '-n', '1', matchList[0]], capture_output=True)
  57. #print(str(i) + " after ping")
  58. #time.sleep(0.01)
  59. #cmdOutput = ping.stdout.decode()
  60. #matchList[2] = ''.join(re.findall('time=(\d+)', cmdOutput))
  61. print(str(i) + " before ping")
  62. graph_value = ping() # int(matchList[2])
  63. print(str(i) + " after ping")
  64.  
  65. if graph_value > MAX_PING:
  66. graph_value = MAX_PING
  67. if graph_value < 0:
  68. graph_value = 0
  69.  
  70. new_x, new_y = i, graph_value
  71.  
  72. if i >= NO_OF_PINGS:
  73. graph.move(-STEP_SIZE, 0)
  74. prev_x = prev_x - STEP_SIZE
  75.  
  76. graph.draw_line((prev_x, prev_y), (new_x, new_y), color='red')
  77. prev_x, prev_y = new_x, new_y
  78. i += STEP_SIZE if i < NO_OF_PINGS else 0
  79.  
  80. window.close()
  81.  
  82. def ping():
  83. while True:
  84. try:
  85. if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
  86. ping = subprocess.run(['ping', '-c', '1', matchList[0]], capture_output=True)
  87. elif platform == 'win32':
  88. ping = subprocess.run(['ping', '-n', '1', matchList[0]], capture_output=True)
  89. break
  90. except:
  91. continue
  92. cmdOutput = ping.stdout.decode()
  93. return int(''.join(re.findall('time=(\d+)', cmdOutput)))
  94.  
  95.  
  96. #if __name__ == '__main__':
  97. # main_loop()
  98.  
  99. main_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement