您的位置:首页 > 编程语言 > Python开发

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

2013-01-13 17:52 519 查看
# -*- 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[i].start()

for i in nloops:

threads[i].join()

print "all done at:",ctime()

##开始

main('10.10.29',255)

[/i][/i]
本文出自 “90后运维博客” 博客,请务必保留此出处http://scpman.blog.51cto.com/5060169/1117472
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: