码迷,mamicode.com
首页 > 编程语言 > 详细

python 监测主机是否alive

时间:2015-09-22 14:45:14      阅读:341      评论:0      收藏:0      [点我收藏+]

标签:python   null   import   主机   

#!/usr/bin/env python

#-*- coding: utf-8 -*-


import sys

from threading import Thread

import subprocess

from Queue import Queue



num_threads=3

ips = [‘127.0.0.1‘,‘192.168.20.140‘]

q=Queue()


def pingme(i,queue):

    while True:

        ip = queue.get()

        print ‘Thread %s pinging %s‘ %(i,ip)

        ret = subprocess.call(‘ping -c 1 %s‘ % ip,shell=True,stdout=open(‘/dev/null‘,‘w‘),stderr=subprocess.STDOUT)

        if ret == 0:

            print ‘%s is alive!‘ % ip

        elif ret ==1:

            print ‘%s is down...‘ % ip

        queue.task_done()


for i in range(num_threads):

    t = Thread(target=pingme,args=(i,q))

    t.setDaemon(True)

    t.start()


for ip in ips:

    q.put(ip)

print ‘main thread waiting..‘

q.join();print ‘done‘



显示:

main thread waiting..

Thread 0 pinging 127.0.0.1

Thread 1 pinging 192.168.20.140

192.168.20.140 is alive!

127.0.0.1 is alive!

done


python 监测主机是否alive

标签:python   null   import   主机   

原文地址:http://yzg784534398.blog.51cto.com/9719002/1697020

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!