Skip to content

Commit 4ca9856

Browse files
Create pinging_with_threads_2.py
1 parent 446c529 commit 4ca9856

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

scripts/pinging_with_threads_2.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
import os, re, threading
3+
4+
class ip_check(threading.Thread):
5+
def __init__ (self,ip):
6+
threading.Thread.__init__(self)
7+
self.ip = ip
8+
self.__successful_pings = -1
9+
def run(self):
10+
ping_out = os.popen("ping -q -c2 "+self.ip,"r")
11+
while True:
12+
line = ping_out.readline()
13+
if not line: break
14+
n_received = re.findall(received_packages,line)
15+
if n_received:
16+
self.__successful_pings = int(n_received[0])
17+
def status(self):
18+
if self.__successful_pings == 0:
19+
return "no response"
20+
elif self.__successful_pings == 1:
21+
return "alive, but 50 % package loss"
22+
elif self.__successful_pings == 2:
23+
return "alive"
24+
else:
25+
return "shouldn't occur"
26+
received_packages = re.compile(r"(\d) received")
27+
28+
check_results = []
29+
for suffix in range(20,70):
30+
ip = "192.168.178."+str(suffix)
31+
current = ip_check(ip)
32+
check_results.append(current)
33+
current.start()
34+
35+
for el in check_results:
36+
el.join()
37+
print "Status from ", el.ip,"is",el.status()
38+

0 commit comments

Comments
 (0)