-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingcheck.py
36 lines (30 loc) · 1.01 KB
/
pingcheck.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
#!/usr/bin/env python3
# Author: Piero Toffanin
# License: AGPLv3
import os
import argparse
import subprocess
import time
parser = argparse.ArgumentParser(description='Utility to ping hosts and check for network issues ')
parser.add_argument('--hosts',
type=str,
default="hosts.txt",
help='Path to file with list of hosts to check (.txt)')
parser.add_argument('--retries',
type=int,
default=10,
help='Number of retries before erroring out')
args = parser.parse_args()
with open(args.hosts, 'r') as f:
hosts = [h.strip() for h in f.read().split() if h.strip() != '']
for host in hosts:
online = False
for i in range(args.retries):
try:
subprocess.run(['ping', '-c', '1', host], check=True, stdout = subprocess.DEVNULL, stderr = subprocess.DEVNULL)
online = True
break
except:
time.sleep(1 * i)
if not online:
print(host + " is not responding")