原理是监控端口的形式,持续进行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)
原文地址:https://blog.51cto.com/swiki/2415135
时间: 2024-10-04 10:35:57