This repository has been archived by the owner on Dec 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
generic-ping-test.py
248 lines (238 loc) · 6.43 KB
/
generic-ping-test.py
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
"""
/* Developer: SahanaJayaram, email: sjayaramu@eplus.com */
"""
import os
import platform
import subprocess
plat = platform.system()
def ping_return_code(hostname):
"""Use the ping utility to attempt to reach the host. We send 5 packets
('-c 5') and wait 3 milliseconds ('-W 3') for a response. The function
returns the return code from the ping utility.
"""
try:
if plat == "Windows":
# Checking for Windows Servers
response = subprocess.call(['ping', '-n', '5', '-W', '3', hostname],
stdout=open(os.devnull, 'w'),
stderr=open(os.devnull, 'w'))
elif plat == "Linux":
# Checking for Linux Servers
response = subprocess.call(['ping', '-c', '5', '-W', '3', hostname],
stdout=open(os.devnull, 'w'),
stderr=open(os.devnull, 'w'))
print "Verify a host" + " " + '"' + hostname + '"' + " " + "for Network Reachability using PING Tool"
print "---- Trying to Ping a Server with IPAddress ----", hostname
# Check for response status code, Ping Success code is '0'
if response == 0:
print "********************************************************************"
print(hostname, 'is UP and reachable!')
print "********************************************************************"
print "\n"
elif response == 1 or 2 or 256 or 512:
print "********************************************************************"
print(hostname, 'is DOWN and No response from Server!')
print "********************************************************************"
print "\n"
except Exception:
print "********************************************************************"
print(hostname, 'is DOWN and Host Unreachable!')
print "********************************************************************"
print "\n"
return response
def verify_hosts(host_list):
"""For each hostname in the list, attempt to reach it using ping. Returns a
dict in which the keys are the hostnames, and the values are the return
codes from ping. Assumes that the hostnames are valid.
"""
return_codes = dict()
for hostname in host_list:
return_codes[hostname] = ping_return_code(hostname)
return return_codes
def main():
# List of IP-address/Hosts for Ping test
hosts_to_test = [
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'192.168.1.159',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.63',
'10.1.185.64',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.78',
'10.1.185.80',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.82',
'10.1.185.83',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.90',
'10.1.185.92',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.51',
'10.1.185.52',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.185.53',
'10.1.185.56',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
'127.0.0.1',
'google.com',
'127.0.0.2',
'10.1.1.1',
'10.1.185.71',
'10.1.185.62',
]
verify_hosts(hosts_to_test)
if __name__ == '__main__':
main()