Python Ip address monitoring tool (open source) written with help of AI and tested by me
Posted: Fri Feb 14, 2025 1:56 pm
Python Ip address monitoring tool with alarm


Code: Select all
import tkinter as tk
from tkinter import ttk
import threading
import subprocess
import os
import time
import winsound # For Windows beep (remove for Linux)
# Linux alternative included in code
class PingMonitorApp:
def __init__(self, root):
self.root = root
self.root.title("Ping Monitor")
# Variables
self.target_ip = tk.StringVar()
self.interval = tk.StringVar(value="60")
self.monitoring = False
self.beep_active = False
# GUI Layout
ttk.Label(root, text="IP Address:").grid(row=0, column=0, padx=5, pady=5)
self.ip_entry = ttk.Entry(root, textvariable=self.target_ip)
self.ip_entry.grid(row=0, column=1, padx=5, pady=5)
ttk.Label(root, text="Interval (seconds):").grid(row=1, column=0, padx=5, pady=5)
self.interval_entry = ttk.Entry(root, textvariable=self.interval)
self.interval_entry.grid(row=1, column=1, padx=5, pady=5)
self.start_button = ttk.Button(root, text="Start Monitoring", command=self.toggle_monitoring)
self.start_button.grid(row=2, column=0, columnspan=2, padx=5, pady=5)
self.status_label = ttk.Label(root, text="Status: Stopped")
self.status_label.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
def toggle_monitoring(self):
if self.monitoring:
self.stop_monitoring()
else:
self.start_monitoring()
def start_monitoring(self):
ip = self.target_ip.get()
if not ip:
self.update_status("Error: IP address required")
return
try:
interval = int(self.interval.get())
except ValueError:
self.update_status("Error: Interval must be a number")
return
self.monitoring = True
self.start_button.config(text="Stop Monitoring")
self.update_status("Monitoring started...")
# Start monitoring thread
threading.Thread(target=self.monitor_loop, args=(ip, interval), daemon=True).start()
def stop_monitoring(self):
self.monitoring = False
self.start_button.config(text="Start Monitoring")
self.update_status("Monitoring stopped")
def monitor_loop(self, ip, interval):
while self.monitoring:
if self.ping_ip(ip):
self.beep_active = False
self.update_status(f"{time.ctime()}: Ping successful to {ip}")
else:
self.beep_active = True
self.update_status(f"{time.ctime()}: Ping failed to {ip}")
# Start beep in separate thread
threading.Thread(target=self.beep_loop, daemon=True).start()
time.sleep(interval)
def ping_ip(self, ip):
try:
output = subprocess.check_output(
['ping', '-c', '1', '-W', '5', ip],
stderr=subprocess.STDOUT,
universal_newlines=True
)
return True
except subprocess.CalledProcessError:
return False
def beep_loop(self):
while self.beep_active and self.monitoring:
# For Linux (comment out if using Windows)
os.system('echo -e "\a"') # Terminal bell
# For Windows (uncomment):
# winsound.Beep(1000, 500) # Frequency 1000Hz, duration 500ms
time.sleep(1)
def update_status(self, message):
self.root.after(0, lambda: self.status_label.config(text=message))
if __name__ == "__main__":
root = tk.Tk()
app = PingMonitorApp(root)
root.mainloop()