-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping-trace.py
More file actions
72 lines (49 loc) · 1.71 KB
/
ping-trace.py
File metadata and controls
72 lines (49 loc) · 1.71 KB
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
import os
from concurrent.futures import ThreadPoolExecutor
from subprocess import check_output, CalledProcessError
import logging
import sys
import time
CURRENT_DIR = os.getcwd()
START_TIME = time.time()
MAX_THREADS = 5
FILENAME = 'devices.txt'
PING_COMMAND = 'ping -q -c 3 -W 1'
TRACEROUTE_COMMAND = 'traceroute'
formatter = '%(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(stream=sys.stdout, format=formatter, level=logging.DEBUG)
def title(section_name):
section_title = '\n' + '*' * 70 + f'\n{section_name}\n' + '*' * 70 + '\n'
return section_title
def health_checks(ip):
ping_cmd = f'{PING_COMMAND} {ip}'
trace_cmd = f'{TRACEROUTE_COMMAND} {ip}'
ping_status = run_command(ping_cmd)
trace_status = run_command(trace_cmd)
filename = f'{CURRENT_DIR}{os.sep}{ip}.txt'
with open(filename, 'w') as f:
f.write(title('Ping Results:'))
f.write(ping_status)
f.write(title('Trace Results:'))
f.write(trace_status)
logging.info(f'Wrote outputs to: {filename}')
logging.debug(ping_status)
logging.debug(trace_status)
def run_command(command):
logging.info(f'running: {command}')
split_cmd = command.split()
try:
output = check_output(split_cmd).decode('utf-8')
except CalledProcessError:
return 'FAILED'
return output
def main():
with open(FILENAME, 'r') as f:
ips = f.read().splitlines()
num_ips = len(ips)
with ThreadPoolExecutor(max_workers=MAX_THREADS) as executor:
[executor.submit(health_checks, ip) for ip in ips]
end_time = time.time() - START_TIME
logging.info(f'Checked {num_ips} hosts in {round(end_time)} seconds.')
if __name__ == '__main__':
main()