Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess, time, re, math
- import PySimpleGUI as sg
- from sys import platform
- STEP_SIZE = 1
- NO_OF_PINGS = 500
- MAX_PING = 250
- CANVAS_SIZE = (800, 600)
- matchList = ['', '', '']
- def main_loop():
- global matchList
- 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()
- 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']
- i = 0
- prev_x, prev_y = 0, 0
- graph_value = 0
- while True:
- event, values = window.read(timeout=10)
- if event == 'Exit' or event is None:
- break
- #time.sleep(0.01)
- #print(str(i) + " before ping")
- #ping = subprocess.run(['ping', '-n', '1', matchList[0]], capture_output=True)
- #print(str(i) + " after ping")
- #time.sleep(0.01)
- #cmdOutput = ping.stdout.decode()
- #matchList[2] = ''.join(re.findall('time=(\d+)', cmdOutput))
- print(str(i) + " before ping")
- graph_value = ping() # int(matchList[2])
- print(str(i) + " after ping")
- 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
- window.close()
- def ping():
- while True:
- try:
- if platform == 'darwin' or platform == 'linux' or platform == 'linux2':
- ping = subprocess.run(['ping', '-c', '1', matchList[0]], capture_output=True)
- elif platform == 'win32':
- ping = subprocess.run(['ping', '-n', '1', matchList[0]], capture_output=True)
- break
- except:
- continue
- cmdOutput = ping.stdout.decode()
- return int(''.join(re.findall('time=(\d+)', cmdOutput)))
- #if __name__ == '__main__':
- # main_loop()
- main_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement