-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_ping_util.pyw
154 lines (117 loc) · 4.75 KB
/
batch_ping_util.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Simple GUI Batch Ping Utility
# 27/06/24 Jamie Drinkell
# Thanks to https://www.abstractapi.com/guides/validate-ip-address-python for the IP checker
from ping3 import ping
import FreeSimpleGUI as gui
import ipaddress
# Globals #
marker_mid_pings = False
start_address = ''
end_address = ''
old_address = ''
timeout = ''
def validate_ip_address(ip_string):
try:
ip_object = ipaddress.ip_address(ip_string)
# window["-INFO-TEXT-"].update("The IP address " + ip_string + " is valid.")
return True
except ValueError:
window["-INFO-TEXT-"].update("The address " + ip_string + " is not valid")
return False
def calculate_next_address(address):
if address == end_address:
return "PING_END_ADDRESS"
to_ping_by_parts = address.split('.')
for y in range(3, 0, -1):
if int(to_ping_by_parts[y]) >= 255:
if y == 0:
print("Can't go up from 255.255.255.255 !")
return
else:
# This needs set to zero because we increment the higher part in the next loop
to_ping_by_parts[y] = '0'
continue
else:
to_ping_by_parts[y] = str(int(to_ping_by_parts[y]) + 1)
break
address_to_ping = '.'.join(str(e) for e in to_ping_by_parts)
return address_to_ping
def do_ping(address, timeout):
window["-INFO-TEXT-"].update("Pinging address " + str(address))
window.Refresh()
response = ping(address, timeout=timeout)
if response is None or False:
window["-INFO-TEXT-"].update("Failed to reach " + str(address))
window.Refresh()
else:
window["-INFO-TEXT-"].update("Pinged " + str(address) + " in " + str(response))
print("Pinged " + str(address) + " in " + str(response))
window.Refresh()
# MAIN #
gui.theme('DarkAmber')
layout = [[gui.Text("Enter First Address:", size=(27, 1)), gui.InputText()],
[gui.Text("Enter Last Address:", size=(27, 1)), gui.InputText()],
[gui.Text("Enter Timeout (Default = 1s):", size=(27, 1)), gui.InputText()],
[gui.Text("Current ping info displayed here.", key="-INFO-TEXT-", size=(27, 1)), gui.Output(size=(43, 8))],
[gui.Button("Start", key="-START-BUTTON-"), gui.Button("Cancel", key="-CANCEL-BUTTON-", disabled=True)]]
window = gui.Window('Batch Ping Utility', layout, finalize=True)
print("Good pings will be recorded here.")
while True:
event, values = window.read(timeout=15)
if event == gui.WIN_CLOSED:
break
elif event == "-CANCEL-BUTTON-":
window["-INFO-TEXT-"].update("Cancelling")
marker_mid_pings = False
start_address = ''
end_address = ''
timeout = ''
window["-INFO-TEXT-"].update("Cancelled")
window["-START-BUTTON-"].update(disabled=False)
window["-CANCEL-BUTTON-"].update(disabled=True)
elif marker_mid_pings:
next_address = calculate_next_address(old_address)
if next_address == "PING_END_ADDRESS":
do_ping(end_address, timeout)
marker_mid_pings = False
start_address = ''
end_address = ''
timeout = ''
window["-START-BUTTON-"].update(disabled=False)
window["-CANCEL-BUTTON-"].update(disabled=True)
else:
do_ping(next_address, timeout)
old_address = next_address
elif event == "-START-BUTTON-":
start_address = values[0]
end_address = values[1]
timeout = values[2]
if start_address == '':
window["-INFO-TEXT-"].update("Please enter valid start address!")
window.Refresh()
continue
if end_address == '':
window["-INFO-TEXT-"].update("Please enter a valid end address!")
window.Refresh()
continue
if not validate_ip_address(start_address):
continue
if not validate_ip_address(end_address):
continue
if timeout == '':
window["-INFO-TEXT-"].update("NB: Timeout is now 1s")
timeout = 1
window.Refresh()
window["-START-BUTTON-"].update(disabled=True)
window["-CANCEL-BUTTON-"].update(disabled=False)
if start_address == end_address:
marker_mid_pings = False
do_ping(start_address, timeout)
window["-START-BUTTON-"].update(disabled=False)
window["-CANCEL-BUTTON-"].update(disabled=True)
else:
marker_mid_pings = True
do_ping(start_address, timeout)
old_address = start_address
window.Refresh()
window.close()