#!/usr/bin/env python
#_*_coding:utf-8_*_
import pexpect
import datetime
import threading
import os
def Log(log):
f = open('log.txt', 'a+')
f.writelines('%s\n' % log)
f.close()
class PING(threading.Thread):
def __init__(self,ip):
threading.Thread.__init__(self)
self.ip = ip
def run(self):
starttime = datetime.datetime.now()
ping=pexpect.spawn("ping -c1 %s" % (self.ip))
try:
check = ping.expect([pexpect.TIMEOUT,"1 packets transmitted, 1 received, 0% packet loss"],10)
if check == 1:
Log('%s normal %s' % (starttime,self.ip))
elif check == 0:
Log('%s timeout %s' % (starttime,self.ip))
except Exception,e:
Log('%s Error %s' % (starttime,self.ip))
os.remove('log.txt')
Thread_list=[]
f = open('iplist','r')
for line in f:
line=line.strip('\n')
ip=line.split()[0]
t = PING(ip)
Thread_list.append(t)
for i in range(len(Thread_list)):
Thread_list[i].start()