首页 > 编程语言 > 详细

python 网络监控主机

时间:2019-03-24 13:57:02      阅读:147      评论:0      收藏:0      [点我收藏+]
import subprocess
import threading
import time
from queue import Queue
from queue import Empty

def wrapper(func):
    def inner(*args,**kwargs):
        s1 = time.time()
        func(*args,**kwargs)
        s2 = time.time()
        s3 = s2 - s1
        print(s3)
    return inner


def run_ping(ip):
    proc = subprocess.run(ping -n 1 -w 2 {}.format(ip),
                          shell=True,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE,
                          )
    return_code = proc.returncode
    # print(return_code)
    if return_code == 0:
        print({} 网络良好!.format(ip))
    else:
        print({} 网络故障!.format(ip))


def is_reacheable(q):
    try:
        while True:
            ip = q.get_nowait()
            run_ping(ip)
    except Empty:
        pass

@wrapper
def main():
    q = Queue()
    with open(ip.txt) as f:
        for line in f:
            q.put(line)
    threads = []
    for i in range(10):
        thr = threading.Thread(target=is_reacheable, args=(q,))
        thr.start()
        threads.append(thr)
    for thr in threads:
        thr.join()


if __name__ == __main__:
    main()
    # run_ping(‘12.0.0.11‘)

 

python 网络监控主机

原文:https://www.cnblogs.com/superniao/p/10587866.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!