python 多线程检测同网段主机在线状态

 # -*- coding: utf-8 -*-
#code by scpman
#http://www.scpman.com
import threading
from time import sleep,ctime
def ping_all(i,ip):
import os
#print ip
cmd="ping -n 1 "+ip+'|findstr TTL'
#print cmd
res=os.popen(cmd)
for res in res.readlines():
#print res
print 'HOST:',ip,'is alive!\n'
def main(ips,end):
print 'starting at:',ctime()
threads=[]

##########################
ips=ips+'.'
end=end
ip=[]
for s in range(end):
ip.append(ips+str(s))
nloops=range(len(ip))
####################

for i in nloops:

t=threading.Thread(target=ping_all,args=(i,ip))
threads.append(t)
for i in nloops:
threads.start()

for i in nloops:
threads.join()
print "all done at:",ctime()

##开始
main('10.10.29',255)

你可能感兴趣的:(多线程,python,检测,主机,在线状态)