GUI-based tool for monitoring network connectivity through ping tests
July 22, 2023
This Python script is a simple GUI-based tool for monitoring network connectivity through ping tests. It uses the Tkinter library for the graphical user interface (GUI) and subprocess to run ping commands. Let’s break down the code:
Importing Libraries
import subprocess
from datetime import datetime, timedelta
import time
from tkinter import *
import ipaddress
import re
- The script imports necessary libraries, including subprocess for running external commands, datetime for handling timestamps, time for managing time-related functions, Tkinter for creating the GUI, and ipaddress and re for handling IP addresses and regular expressions.
Function Definitions
def start_ping():
# Function to start the ping test
# ... (explained below)
def abort_ping():
# Function to abort the ping test (currently empty)
pass
- The script defines two functions:
start_ping
to initiate the ping test andabort_ping
to abort it (currently empty).
GUI Initialization
root = Tk()
root.title("Ping Monitor by School of Iris")
label_1 = Label(root, font="Arial", text="Enter IP or URL", anchor=W)
label_1.grid(row=0, column=0)
ipaddr = Entry(root, font="Arial", width=30, borderwidth=2)
ipaddr.grid(row=0, column=1, columnspan=2)
label_2 = Label(root, font="Arial", text="Enter Device Name", anchor=W)
label_2.grid(row=1, column=0)
device = Entry(root, font="Arial", width=30, borderwidth=2)
device.grid(row=1, column=1, columnspan=2)
label_3 = Label(root, font="Arial", text="Enter Duration (minutes)", anchor=W)
label_3.grid(row=2, column=0)
duration = Entry(root, font="Arial", width=30, borderwidth=2)
duration.grid(row=2, column=1, columnspan=2)
button_start = Button(root, font="Arial", text="START", padx=50, command=start_ping)
# button_abort = Button(root, font="Arial", text="Abort", padx=20, command=abort_ping)
# button_exit = Button(root, font="Arial", text="Exit", padx=20, command=root.quit)
button_start.grid(row=3, column=0, columnspan=3)
# button_abort.grid(row=3, column=1)
# button_exit.grid(row=3, column=2)
statusbar = Label(root, text="Click START for monitoring", bd=1, relief=SUNKEN, anchor=W)
statusbar.grid(row=5, column=0, sticky=W + E, columnspan=3)
- The script initializes a Tkinter window (
root
) and creates labels, entry fields, and buttons for the GUI.
Function start_ping
def start_ping():
# ... (previous lines)
if deviceName and input_IP and minutes:
if (ip_check or url_check or url2_check):
if min_check:
# ... (previous lines)
while datetime.now() <= endTime:
result = subprocess.run(pcommand, capture_output=True, text=True)
# ... (previous lines)
if result.returncode == 1 and startevent == 0:
# ... (previous lines)
elif result.returncode == 1 and startevent == 1:
pass
elif result.returncode == 0 and startevent == 1:
# ... (previous lines)
elif result.returncode == 0 and startevent == 0:
# ... (previous lines)
statusbar.configure(text='Program Ended')
# ... (previous lines)
else:
# ... (previous lines)
- The
start_ping
function is called when the “START” button is clicked. It retrieves input values, checks their validity, and initiates a ping test for the specified duration. The ping results are logged into a file, and the program’s progress is displayed in the status bar.
GUI Main Loop
root.mainloop()
- The script enters the Tkinter main loop to handle user interactions and events.
In summary, this script creates a simple GUI using Tkinter to perform ping tests on a specified IP address or URL for a specified duration. It uses subprocess to run the ping command and logs the results to a text file. The GUI allows users to input the target, device name, and test duration, and then start the ping test.