-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathping.py
125 lines (103 loc) · 3.04 KB
/
ping.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
#!/usr/bin/env python3
import os
import platform
import requests
from apscheduler.schedulers.background import BackgroundScheduler
import time
import json
import datetime
import urllib.error
internet = ["8.8.8.8",
"23.72.27.241",
"23.72.15.162",
"107.162.132.24",
"8.8.4.4",
"198.41.209.137",
"209.244.0.3",
"208.67.220.220",
"72.32.138.96",
"75.75.75.75",
"204.79.197.203",
"139.130.4.5",
"208.67.222.222",
"209.244.0.4",
"64.6.64.6",
"64.6.65.6",
"84.200.69.80",
"84.200.70.40",
"8.26.56.26",
"8.20.247.20",
"208.67.222.222",
"208.67.220.220",
"209.88.198.133",
"195.46.39.39",
"195.46.39.40",
"162.211.64.20",
"208.76.50.50",
"216.146.35.35",
"216.146.36.36",
"37.235.1.174",
"37.235.1.177",
"198.101.242.72",
"23.253.163.53",
"77.88.8.8",
"77.88.8.1",
"91.239.100.100",
"89.233.43.71",
"74.82.42.42"]
connectivity = {}
switches = {}
aps = {}
count = 0
def ping(host):
if platform.system().lower() == "windows":
return os.system("ping -n 1 -w 2000 " + host + " > nul") == 0
else:
return os.system("ping -c 1 -W 2 " + host + " > /dev/null 2>&1") == 0
def pingInternet(host):
if platform.system().lower() == "windows":
return os.system("ping -n 4 -w 2000 " + host + " > nul") == 0
else:
return os.system("ping -c 4 -W 2 " + host + " > /dev/null 2>&1") == 0
def make_call(data):
url = 'http://127.0.0.1:3000/pings'
payload = data
headers = {'content-type': 'application/json'}
try:
r = requests.post(url, data=json.dumps(payload), headers=headers)
r.raise_for_status()
except (requests.exceptions.RequestException, TimeoutError, ConnectionError, ConnectionRefusedError,
urllib.error.URLError) as e:
print(e)
def do_work_son():
global count
arr = [{}, []]
i = "Up" if pingInternet(internet[count]) else "Down"
if i == "Down":
print(internet[count])
arr[0]['int'] = i
count += 1
count = 0 if count == len(internet) else count
for con in connectivity:
i = "Up" if ping(connectivity[con]) else "Down"
arr[0][con] = i
for switch in switches:
p = ping(switches[switch])
if not p:
arr[1].append(switch)
for ap in aps:
p = ping(aps[ap])
if not p:
arr[1].append(ap)
make_call(arr)
print("Running - " + datetime.datetime.now().strftime('%c'))
do_work_son()
if __name__ == '__main__':
scheduler = BackgroundScheduler()
scheduler.add_job(do_work_son, 'interval', minutes=1)
scheduler.start()
try:
while True:
time.sleep(2)
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()