# -*- 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