首页 > 微信 > 详细

Python写一个网络监控小程序

时间:2019-06-28 21:26:45      阅读:154      评论:0      收藏:0      [点我收藏+]
原理是监控端口的形式,持续进行telnet判断,若端口无法连接说明网络出现异常,则进行打印到本地文件

# -*- coding: utf-8 -*
import telnetlib,time,os

Path = os.getcwd()
file_name = ‘ping_log.txt‘
ping_log_path = os.path.join(Path,file_name)

if not os.path.exists(ping_log_path):
    os.mknod(file_name)

while True:
    try:
        tn=telnetlib.Telnet(‘192.168.2.128‘,80)
    except Exception as e:
        with open(ping_log_path,‘a‘,encoding=‘utf-8‘) as f:
            f.write(time.strftime("%b %d %Y %H:%M:%S", time.localtime()) + ‘网络异常\n‘)
    else:
        continue

    time.sleep(1)

Python写一个网络监控小程序

原文:https://blog.51cto.com/swiki/2415135

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