Python寫一個網絡監控小程序

原理是監控端口的形式,持續進行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)
相關文章
相關標籤/搜索