PythonTip >> 博文 >> chinaunix

python-pingall

zihua 2014-01-13 18:01:53 点击: 778 | 收藏


import re
import sys
import platform


num_threads = 10
queue = Queue()


def pinger(i,q):
    while True:
        ip=q.get()
        if platform.system() == 'Linux':
            cmd="ping -c 1 %s"%ip
            outfile="/dev/null"
        elif platform.system() == "windows":
            cmd = "ping -n 1 %s"%ip
            outfile="ping.temp"
        ret = subprocess.call(cmd,shell=True,stdout=open(outfile,'w'),stderr=subprocess.STDOUT)
        if ret == 0:
            print "%s:is alive"%ip
        else:
            print "%s is down"%ip
        q.task_done()


for i in range(num_threads):
    worker = Thread(target=pinger,args=(i,queue))
    worker.setDaemon(True)
    worker.start()




host_file =open(r'hosts.txt','r')
ips = []
re_obj=re.compile(r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")
for line in host_file:
    for match in re_obj.findall(line):
        ips.append(match)
host_file.close()


for ip in ips:
    queue.put(ip)


print "Main Thread Waiting"
queue.join()
print "Done"


result =raw_input("Please press any key to exit")
if result:
    sys.exit(0)

原文链接:http://blog.chinaunix.net/uid-25602770-id-4011455.html

作者:zihua | 分类: chinaunix | 标签: python | 阅读: 778 | 发布于: 2014-01-13 18时 |
上一篇:python-sftp
下一篇: python-tar-压缩管理