Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. import subprocess, time, re, math
  2. import PySimpleGUI as sg
  3.  
  4. STEP_SIZE = 1
  5. NO_OF_PINGS = 500
  6. MAX_PING = 250
  7. CANVAS_SIZE = (800, 600)
  8. matchList = ['', '', '']
  9.  
  10. def main_loop():
  11. sg.change_look_and_feel('Dark Blue 3')
  12. layout = [
  13. [sg.Text('Enter domain name of what you want to ping')],
  14. [sg.Input(size=(37, 1), key='dn')],
  15. [sg.Ok(), sg.Cancel()]
  16. ]
  17.  
  18. window = sg.Window('Domain name entry', layout)
  19. event, value = window.read()
  20. if event is None or event == 'Cancel':
  21. return
  22.  
  23. matchList[0] = value['dn']
  24. matchList[1] = ''.join(re.findall('Ping statistics for (\d+.\d+.\d+.\d+)', subprocess.run(['ping', '-n', '1', matchList[0]], capture_output=True).stdout.decode()))
  25.  
  26. window.close()
  27.  
  28. sg.set_options(element_padding=(0,0))
  29.  
  30. sg.change_look_and_feel('Dark Blue 3')
  31. layout = [
  32. [sg.Text("The IP address of hostname '" + matchList[0] + "' is: " + matchList[1], size=(100, 1), )],
  33. [sg.Graph(CANVAS_SIZE, (0, 0), (NO_OF_PINGS, MAX_PING),
  34. background_color='white', key='graph')],
  35. [sg.Button('Exit', button_color=('black', 'white'))]
  36. ]
  37.  
  38. window = sg.Window(matchList[0], layout, grab_anywhere=True, no_titlebar=False, use_default_focus=False, finalize=True)
  39. graph = window['graph']
  40.  
  41. prev_response_time = None
  42. i = 0
  43. prev_x, prev_y = 0, 0
  44. graph_value = 0
  45. while True:
  46. event, values = window.read(timeout=1000)
  47. if event == 'Exit' or event is None:
  48. break
  49.  
  50. ping = subprocess.run(['ping', '-n', '1', matchList[0]], capture_output=True)
  51. cmdOutput = ping.stdout.decode()
  52. matchList[2] = ''.join(re.findall('time=(\d+)', cmdOutput))
  53. graph_value = int(matchList[2])
  54.  
  55. if graph_value > MAX_PING:
  56. graph_value = MAX_PING
  57. if graph_value < 0:
  58. graph_value = 0
  59.  
  60. new_x, new_y = i, graph_value
  61.  
  62. if i >= NO_OF_PINGS:
  63. graph.move(-STEP_SIZE, 0)
  64. prev_x = prev_x - STEP_SIZE
  65.  
  66. graph.draw_line((prev_x, prev_y), (new_x, new_y), color='red')
  67. prev_x, prev_y = new_x, new_y
  68. i += STEP_SIZE if i < NO_OF_PINGS else 0
  69.  
  70. window.close()
  71.  
  72.  
  73. if __name__ == '__main__':
  74. main_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement