单线程版本:
#!/usr/bin/python
import os
import commands
f=open("/etc/hosts","r")
for line in f.readlines():
ip=str(line.split()[0:1][0])
if ip == '#' or ip == ' ':
continue
status, output = commands.getstatusoutput('ping -c 3 -w 5' +" " + ip)
if status == 0:
continue
else:
print "Warnning:" + ip + " is timed out!!!"
f.close
多线程版本:
#!/usr/bin/python
import os,re,sys
from threading import Thread
class testit(Thread):
def __init__ (self,ip):
Thread.__init__(self)
self.ip = ip
self.status = -1
def run(self):
pingaling = os.popen("ping -w5 -c2 "+self.ip,"r")
while 1:
line = pingaling.readline()
if not line: break
igot = re.findall(testit.lifeline,line)
if igot:
self.status = int(igot[0])
testit.lifeline = re.compile(r"(\d) received")
pinglist = []
f=open("/etc/hosts","r")
for line in f.readlines():
ip = str(line.split()[0:1][0])
if ip == '#' or ip == '':
continue
current = testit(ip)
pinglist.append(current)
current.start()
for pingle in pinglist:
pingle.join()
if pingle.status == 0:
print pingle.ip + "is timed out!!"
没有评论:
发表评论