forked from Yousuf9963/Check-Host
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck-host.py
381 lines (191 loc) · 11.3 KB
/
check-host.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import sys
import os
import json
import requests
import time
from prettytable import PrettyTable
# COLORS #
RED = "\u001b[31m"
GREEN = "\u001b[32m"
YELLOW = "\u001b[33m"
RESET = "\u001b[0m"
# COLORS #
method_list = ["iplook", "ping", "http", "tcp", "dns"]
help_menu = """ iplook: website location search, IP address
Usage:
| python3 check-host.py iplook <target>
examples:
| python3 check-host.py iplook 1.1.1.1.2.3.9.1.1
| python3 check-host.py iplook example.com
only for domain, IP address!
ping: Checking the integrity and quality of connections
Usage:
| python3 check-host.py ping <target>
examples:
| python3 check-host.py ping 1.1.1.1.2.3.9.1.1
| python3 check-host.py ping http://123example.com
| python3 check-host.py ping https://123example.com
| python3 check-host.py ping 123example.com
http: Website availability and performance check
Usage:
| python3 check-host.py http <target>:<port> <-- automatically port 80
Examples:
| python3 check-host.py http https://1.1.1.1.2.3.9.1.1:443
| python3 check-host.py http http://123example.com
| python3 check-host.py http https://123example.com
| python3 check-host.py http 123example.com:80
tcp: Testing TCP connection of any port of an IP address, website
Usage:
| python3 check-host.py tcp <target>:<port> <-- automatically port 80
Eexamples:
| python3 check-host.py tcp 1.1.1.1.2.3.9.1.1:53
| python3 check-host.py tcp http://123example.com
| python3 check-host.py tcp ssmtp://123smtp.example.com
dns: Website domain monitoring
Usage:
| python3 check-host.py dns <target>
examples:
| python3 check-host.py dns 1.1.1.1.2.3.9.1.1
| python3 check-host.py dns http://123example.com
| python3 check-host.py dns https://123example.com
| python3 check-host.py dns 123example.com"""
def LOGO_PART():
if sys.platform == "win32": os.system("cls")
else: os.system("clear")
logo = f"""
__ __ __ __
____/ / ___ ____/ /______/ / ___ ___ / /_
/ __/ _ \/ -_) __/ '_/___/ _ \/ _ \(_-</ __/
\__/_//_/\__/\__/_/\_\ /_//_/\___/___/\__/ v1.0 / https://github.com/Yousuf9963
{YELLOW}ــــــــﮩ٨ـﮩﮩ٨ـﮩ٨ـﮩﮩ٨ــــ{RESET}
"""
print(logo)
def METHOD_PART():
if user_method not in method_list:
print(f" [{RED}ERROR{RESET}] invalid method")
sys.exit()
if user_method == "iplook":
IP_LOOK_UP_PART()
elif user_method == "ping" or user_method == "http" or user_method == "tcp" or user_method == "dns":
CONNECT_PART()
def IP_LOOK_UP_PART():
table_list = PrettyTable(["title", "data"])
result_key = json.loads(requests.get(f"http://ip-api.com/json/{user_data}").text)
if result_key["status"] == "fail":
print(f" [{RED}ERROR{RESET}] invalid domain, IP address")
sys.exit()
for data_output_one, data_output_two in result_key.items():
table_list.add_row([data_output_one, data_output_two])
print(f" method: IP address lookup, target: {user_data}\n{table_list}")
def CONNECT_PART():
id_key = json.loads(requests.get(f"https://check-host.net/check-{user_method}?host={user_data}&max_nodes=24", headers={"Accept": "application/json"}).text)
RESULT_CONNECT_PART(id_key)
def RESULT_CONNECT_PART(id_key):
result_key = requests.get(f"https://check-host.net/check-result/{id_key['request_id']}").text
if user_method == "ping":
PING_PART_LIST(id_key, result_key)
elif user_method == "http":
HTTP_PART_LIST(id_key, result_key)
elif user_method == "tcp":
TCP_PART_LIST(id_key, result_key)
elif user_method == "dns":
DNS_PART_LIST(id_key, result_key)
def PING_PART_LIST(id_key, result_key):
table_list = PrettyTable(["location", "result", "time", "IP address"])
loading_count = 0
for server_list in json.loads(result_key):
try:
for data_output in json.loads(result_key)[server_list]:
if data_output[0] == None: raise SystemExit
if data_output[0][0] == "OK": data_output[0][0] = f"{GREEN}{data_output[0][0]}{RESET}"
elif data_output[0][0] == "TIMEOUT" or data_output[0][0] == "MALFORMED": data_output[0][0] = data_output[0][0] = f"{RED}{data_output[0][0]}{RESET}"
if data_output[1][0] == "OK": data_output[1][0] = f"{GREEN}{data_output[1][0]}{RESET}"
elif data_output[1][0] == "TIMEOUT" or data_output[1][0] == "MALFORMED": data_output[1][0] = data_output[1][0] = f"{RED}{data_output[1][0]}{RESET}"
if data_output[2][0] == "OK": data_output[2][0] = f"{GREEN}{data_output[2][0]}{RESET}"
elif data_output[2][0] == "TIMEOUT" or data_output[2][0] == "MALFORMED": data_output[2][0] = data_output[2][0] = f"{RED}{data_output[2][0]}{RESET}"
if data_output[3][0] == "OK": data_output[3][0] = f"{GREEN}{data_output[3][0]}{RESET}"
elif data_output[3][0] == "TIMEOUT" or data_output[3][0] == "MALFORMED": data_output[3][0] = data_output[3][0] = f"{RED}{data_output[3][0]}{RESET}"
table_list.add_row([f"{id_key['nodes'][server_list][1]}, {id_key['nodes'][server_list][2]}", f"{data_output[0][0]} / {data_output[1][0]} / {data_output[2][0]} / {data_output[3][0]}",
f"{round(data_output[0][1] * 1000, 1)} ms. / {round(data_output[1][1] * 1000, 1)} ms. / {round(data_output[2][1] * 1000, 1)} ms. / {round(data_output[3][1] * 1000, 1)} ms.", data_output[0][2]])
except TypeError:
table_list.add_row([f"{id_key['nodes'][server_list][1]}, {id_key['nodes'][server_list][2]}", "loading...", "...", "..."])
loading_count =+ 1
except SystemExit:
table_list.add_row([f"{id_key['nodes'][server_list][1]}, {id_key['nodes'][server_list][2]}", "None", "None", "None"])
if loading_count == 1:
print(f" method: PING, target: {user_data}\n{table_list}")
time.sleep(1)
LOGO_PART()
return RESULT_CONNECT_PART(id_key)
print(f" method: PING, target: {user_data}\n{table_list}")
def HTTP_PART_LIST(id_key, result_key):
table_list = PrettyTable(["location", "result", "time", "code", "IP address"])
loading_count = 0
for server_list in json.loads(result_key):
try:
for data_output in json.loads(result_key)[server_list]:
if data_output[3] == None: data_output[3] = "None"
if data_output[3][0] == "2": data_output[3] = f"{GREEN}{data_output[3]}{RESET}"
elif data_output[3][0] == "3": data_output[3] = f"{YELLOW}{data_output[3]}{RESET}"
elif data_output[3][0] == "4" or data_output[3][0] == "5":
data_output[2][0] = f"{RED}{data_output[2]}{RESET}"
data_output[3][0] = f"{RED}{data_output[3]}{RESET}"
table_list.add_row([f"{id_key['nodes'][server_list][1]}, {id_key['nodes'][server_list][2]}", data_output[2], f"{round(data_output[1], 2)} s.", data_output[3], data_output[4]])
except TypeError:
table_list.add_row([f"{id_key['nodes'][server_list][1]}, {id_key['nodes'][server_list][2]}", "loading...", "...", "...", "..."])
loading_count =+ 1
if loading_count == 1:
print(f" method: HTTP, target: {user_data}\n{table_list}")
time.sleep(1)
LOGO_PART()
return RESULT_CONNECT_PART(id_key)
print(f" method: HTTP, target: {user_data}\n{table_list}")
def TCP_PART_LIST(id_key, result_key):
table_list = PrettyTable(["location", "result", "time"])
loading_count = 0
for server_list in json.loads(result_key):
try:
for data_output in json.loads(result_key)[server_list]:
table_list.add_row([f"{id_key['nodes'][server_list][1]}, {id_key['nodes'][server_list][2]}", f"{GREEN}connected{RESET}", f"{round(data_output['time'], 2)} s."])
except TypeError:
table_list.add_row([f"{id_key['nodes'][server_list][1]}, {id_key['nodes'][server_list][2]}", "loading...", "..."])
loading_count =+ 1
except KeyError:
table_list.add_row([f"{id_key['nodes'][server_list][1]}, {id_key['nodes'][server_list][2]}", f"{RED}{data_output['error']}{RESET}", "None"])
if loading_count == 1:
print(f" method: TCP, target: {user_data}\n{table_list}")
time.sleep(1)
LOGO_PART()
return RESULT_CONNECT_PART(id_key)
print(f" method: TCP, target: {user_data}\n{table_list}")
def DNS_PART_LIST(id_key, result_key):
table_list = PrettyTable(["location", "result A record", "result AAAA record", "ttl"])
loading_count = 0
for server_list in json.loads(result_key):
try:
for data_output in json.loads(result_key)[server_list]:
a_record, aaaa_record = ",\n".join(data_output["A"]), ",\n".join(data_output["AAAA"])
if a_record == "": a_record = f"{RED}no A record{RESET}"
if aaaa_record == "": aaaa_record = f"{RED}no AAAA record{RESET}"
table_list.add_row([f"{id_key['nodes'][server_list][1]}, {id_key['nodes'][server_list][2]}", a_record, aaaa_record, time.strftime("%M m. %S s.", time.gmtime(data_output["TTL"]))])
except TypeError:
table_list.add_row([f"{id_key['nodes'][server_list][1]}, {id_key['nodes'][server_list][2]}", "loading...", "loading...", "..."])
loading_count =+ 1
except KeyError:
if data_output["PTR"] == []: data_output["PTR"] = [f"{RED}no A record{RESET}"]
table_list.add_row([f"{id_key['nodes'][server_list][1]}, {id_key['nodes'][server_list][2]}", data_output["PTR"][0], f"{RED}no AAAA record{RESET}", time.strftime("%M m. %S s.", time.gmtime(data_output["TTL"]))])
if loading_count == 1:
print(f" method: DNS, target: {user_data}\n{table_list}")
time.sleep(1)
LOGO_PART()
return RESULT_CONNECT_PART(id_key)
print(f" method: DNS, target: {user_data}\n{table_list}")
if __name__ == '__main__':
LOGO_PART()
try:
user_method, user_data = sys.argv[1].lower(), sys.argv[2].lower()
if user_method == "help": raise IndexError
except IndexError:
print(help_menu)
sys.exit()
METHOD_PART()