This is the fast solution with threads

import os, re, threading

class ip_check(threading.Thread):
     def __init__ (self,ip):
            threading.Thread.__init__(self)
            self.ip = ip
            self.__successful_pings = -1
     def run(self):
            ping_out = os.popen("ping -q -c2 "+self.ip,"r")
            while True:
                line = ping_out.readline()
                if not line: break
                n_received = re.findall(received_packages,line)
                if n_received:
                     self.__successful_pings = int(n_received[0])
     def status(self):
            if self.__successful_pings == 0:
                 return "no response"
            elif self.__successful_pings == 1:
                 return "alive, but 50 % package loss"
            elif self.__successful_pings == 2:
                 return "alive"
            else:
                 return "shouldn't occur"
received_packages = re.compile(r"(\d) received")

check_results = []
for suffix in range(20,70):
     ip = "192.168.178."+str(suffix)
     current = ip_check(ip)
     check_results.append(current)
     current.start()

for el in check_results:
     el.join()
     print "Status from ", el.ip,"is",el.status()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章