python 网络监控主机
作者:互联网
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')
标签:ip,__,python,主机,thr,监控,time,import,def 来源: https://www.cnblogs.com/superniao/p/10587866.html