Python 檢查 DNS 紀錄異常時間與恢復時間

更新 發佈閱讀 6 分鐘

網路磁碟機常遇到 Windows Event 1014

系統上只記錄問題發生點

需要記錄恢復時間點

Windows Event 紀錄 : eventvwr.msc

---

測試方式

  1. 改 DNS
    • netsh interface ip set dns "乙太網路" static 127.0.0.1
    • netsh interface ip set dns "乙太網路" dhcp網路先停用
  1. 改 DNS

---

程式

檢查並記錄 log

import os import socket import datetime import time # === 設定區 === target_host = "IT-NAS" share_path = r"\\IT-NAS\share" log_dir = "C:/Logs" log_file = os.path.join(log_dir, "network_monitor.log") state_file = os.path.join(log_dir, "last_state.txt") time_file = os.path.join(log_dir, "last_state.time") check_interval_sec = 60 os.makedirs(log_dir, exist_ok=True) def get_connection_status(host, share): try: ip = socket.gethostbyname(host) except socket.gaierror: return "DNS_FAIL", None try: socket.create_connection((ip, 80), 3).close() except: return "TCP_FAIL", ip if not os.path.exists(share): return "UNC_FAIL", ip return "OK", ip def write_log(msg): with open(log_file, "a", encoding="utf-8") as f: f.write(msg + "\n") def get_last_state(): if os.path.exists(state_file): with open(state_file, "r") as f: return f.read().strip() return "OK" def set_last_state(state): with open(state_file, "w", encoding="utf-8") as f: f.write(state) def monitor_once(): now = datetime.datetime.now() now_str = now.strftime("%Y-%m-%d %H:%M:%S") status, ip = get_connection_status(target_host, share_path) last_state = get_last_state() if status == "OK" and last_state != "OK": # 剛恢復 if os.path.exists(time_file): with open(time_file, "r") as f: down_start_str = f.read().strip() down_start = datetime.datetime.strptime(down_start_str, "%Y-%m-%d %H:%M:%S") duration = now - down_start mins = int(duration.total_seconds() // 60) secs = int(duration.total_seconds() % 60) else: mins = secs = 0 write_log("===== 網路恢復 =====") write_log(f"時間:{now_str}") write_log(f"IP:{ip}") write_log(f"已恢復連線,離線時長:{mins} 分 {secs} 秒\n") elif status != "OK" and last_state == "OK": # 剛出現異常 write_log("===== 網路異常開始 =====") write_log(f"時間:{now_str}") write_log(f"類型:{status}") write_log(f"IP:{ip if ip else '無法解析'}") write_log(f"詳細:{'DNS 無法解析' if status == 'DNS_FAIL' else '無法連接 TCP 端口' if status == 'TCP_FAIL' else '磁碟路徑無法存取'}\n") with open(time_file, "w", encoding="utf-8") as f: f.write(now_str) # 更新狀態 set_last_state(status) # === 主迴圈 === while True: monitor_once() time.sleep(check_interval_sec)
raw-image
raw-image


留言
avatar-img
留言分享你的想法!
avatar-img
work b的沙龍
2會員
25內容數
Linux 相關 指令 與 情境 分享
work b的沙龍的其他內容
2023/02/10
改檔案修改權 chmod 代號 檔案 改檔案用戶與群組 chown 用戶 檔案
2023/02/10
改檔案修改權 chmod 代號 檔案 改檔案用戶與群組 chown 用戶 檔案
2022/11/28
冒險者 1–4 part 2 http://wonder100000000.blogspot.com/2022/11/1-4-part-2.html 第七話 炎熱對決 上集提要 支援軍透過一系列 配合 , 成功壓制 工廠 守軍 , 路克 與 工頭 也在一對一 單挑 中 , 獲得優勢 , 沙漠天龍與魔
Thumbnail
2022/11/28
冒險者 1–4 part 2 http://wonder100000000.blogspot.com/2022/11/1-4-part-2.html 第七話 炎熱對決 上集提要 支援軍透過一系列 配合 , 成功壓制 工廠 守軍 , 路克 與 工頭 也在一對一 單挑 中 , 獲得優勢 , 沙漠天龍與魔
Thumbnail
2022/11/28
冒險者 1–4 part 1 http://wonder100000000.blogspot.com/2022/11/1-4-part-1.html 1–4 第六話 砲擊 上集提要 沙漠天龍 支援軍與 魔獸工廠 守軍 展開交戰 , 逐漸陷入劣勢 , 工廠 強力守軍 , 三刺客 與 工頭 不斷以實力壓
Thumbnail
2022/11/28
冒險者 1–4 part 1 http://wonder100000000.blogspot.com/2022/11/1-4-part-1.html 1–4 第六話 砲擊 上集提要 沙漠天龍 支援軍與 魔獸工廠 守軍 展開交戰 , 逐漸陷入劣勢 , 工廠 強力守軍 , 三刺客 與 工頭 不斷以實力壓
Thumbnail
看更多
你可能也想看
Thumbnail
還在煩惱平凡日常該如何增添一點小驚喜嗎?全家便利商店這次聯手超萌的馬來貘,推出黑白配色的馬來貘雪糕,不僅外觀吸睛,層次豐富的雙層口味更是讓人一口接一口!本文將帶你探索馬來貘雪糕的多種創意吃法,從簡單的豆漿燕麥碗、藍莓果昔,到大人系的奇亞籽布丁下午茶,讓可愛的馬來貘陪你度過每一餐,增添生活中的小確幸!
Thumbnail
還在煩惱平凡日常該如何增添一點小驚喜嗎?全家便利商店這次聯手超萌的馬來貘,推出黑白配色的馬來貘雪糕,不僅外觀吸睛,層次豐富的雙層口味更是讓人一口接一口!本文將帶你探索馬來貘雪糕的多種創意吃法,從簡單的豆漿燕麥碗、藍莓果昔,到大人系的奇亞籽布丁下午茶,讓可愛的馬來貘陪你度過每一餐,增添生活中的小確幸!
Thumbnail
今日無操作, 不知道是系統更新還是怎樣,過去可以複製貼上的功能被修改掉了,需要思考一下如何快速更新文章了。
Thumbnail
今日無操作, 不知道是系統更新還是怎樣,過去可以複製貼上的功能被修改掉了,需要思考一下如何快速更新文章了。
Thumbnail
在網路速度有限的情況下,依序記錄不斷產生的資訊,能統計使用者在頁面上操作了哪些功能。
Thumbnail
在網路速度有限的情況下,依序記錄不斷產生的資訊,能統計使用者在頁面上操作了哪些功能。
Thumbnail
工具功能 (1) 彈性任意查詢檔案,如對來源目錄設定,檔案修改日期 設定,檔名特定字串或副檔名設定後,自動查出明細,並可展開至各階子目錄處理     (2) 依查詢後結果,可產出 LIST ,提供查詢結果之確認,再依此對檔案作複 (3) 可對檔案作移動,複製至別處,刪除處理,使電腦可騰出硬碟空間
Thumbnail
工具功能 (1) 彈性任意查詢檔案,如對來源目錄設定,檔案修改日期 設定,檔名特定字串或副檔名設定後,自動查出明細,並可展開至各階子目錄處理     (2) 依查詢後結果,可產出 LIST ,提供查詢結果之確認,再依此對檔案作複 (3) 可對檔案作移動,複製至別處,刪除處理,使電腦可騰出硬碟空間
Thumbnail
學習如何使用Python連接MongoDB進行憑證監控,包括建立MongoDB docker-compose、連接MongoDB、讀取yaml並寫入MongoDB、傳入env以及domain寫入MongoDB、讀取MongoDB、修改MongoDB、刪除MongoDB。
Thumbnail
學習如何使用Python連接MongoDB進行憑證監控,包括建立MongoDB docker-compose、連接MongoDB、讀取yaml並寫入MongoDB、傳入env以及domain寫入MongoDB、讀取MongoDB、修改MongoDB、刪除MongoDB。
Thumbnail
👨‍💻簡介 最近因為憑證越來越多,需要監控什麼時候到期,當到期時發送到期通知,因此撰寫一個簡單的小程式來完成。 這次使用Python和Telegram Bot來監控SSL證書的到期時間並發送通知。並使用GCP工具,如CloudFunction和CloudScheduler做部署平台。
Thumbnail
👨‍💻簡介 最近因為憑證越來越多,需要監控什麼時候到期,當到期時發送到期通知,因此撰寫一個簡單的小程式來完成。 這次使用Python和Telegram Bot來監控SSL證書的到期時間並發送通知。並使用GCP工具,如CloudFunction和CloudScheduler做部署平台。
Thumbnail
每日自動檢查資料庫運作所產生的訊息,若發現有錯誤,自動寄出警告信給擔當人員
Thumbnail
每日自動檢查資料庫運作所產生的訊息,若發現有錯誤,自動寄出警告信給擔當人員
Thumbnail
整合測試的時候突然遇到一個突然無法登入產品網站的問題,把程式模組單獨拉出來測試又正常,觀察測試報告後發現出現發生登入異常的時間點並不固定,而且只要發生就會連續發生一段時間,程式被中斷掉。後來確認問題在...
Thumbnail
整合測試的時候突然遇到一個突然無法登入產品網站的問題,把程式模組單獨拉出來測試又正常,觀察測試報告後發現出現發生登入異常的時間點並不固定,而且只要發生就會連續發生一段時間,程式被中斷掉。後來確認問題在...
追蹤感興趣的內容從 Google News 追蹤更多 vocus 的最新精選內容追蹤 Google News