Ethernet up / Lan Alert

I wanted to trace a LAN cable, used a laptop at one point and connected misc. ethernet cables to a switch. If than LAN goes up a audible alert sounds until unplugged.

Install phyton, pip install psutil.

 

import psutil   # Retrieves hardware status (network interfaces)
import time     # Manages timing and delays
import platform # Detects if the system is Windows, Linux, or macOS
def play_alert():
    “””Triggers a high-pitched audible alert.”””
    if platform.system() == ‘Windows’:
        import winsound
        # Frequency: 2500Hz (High pitch), Duration: 500ms (Half second)
        winsound.Beep(2500, 500)
    else:
        # Fallback for non-Windows systems using the terminal bell
        print(‘\a’, end=”, flush=True)
        time.sleep(0.5)
def monitor_ethernet():
    “””Main loop to detect physical Ethernet link status.”””
    # Get the initial list of network interfaces
    stats = psutil.net_if_stats()
    # Filter for physical Ethernet ports (ignores Wi-Fi/Virtual adapters)
    # Common Ethernet naming: ‘Ethernet’, ‘eth0’, ‘enp0s3’, ‘en0’
    eth_interfaces = [
        iface for iface in stats.keys()
        if ‘eth’ in iface.lower() or ‘ethernet’ in iface.lower() or ‘en’ in iface.lower()
    ]
    if not eth_interfaces:
        print(“No Ethernet interfaces detected. Check your Device Manager.”)
        return
    print(f”Monitoring interfaces: {‘, ‘.join(eth_interfaces)}”)
    try:
        while True:
            # Refresh the interface stats in each loop
            current_stats = psutil.net_if_stats()
            # Check if any identified Ethernet port has ‘isup’ as True
            # ‘isup’ becomes True when a physical electrical link is established
            is_any_up = any(
                current_stats[iface].isup
                for iface in eth_interfaces if iface in current_stats
            )
            if is_any_up:
                # \r keeps the output on one line to avoid scrolling
                print(“\r[!] LIVE LINK DETECTED – Alert sounding…     “, end=”, flush=True)
                play_alert()
            else:
                print(“\rAwaiting connection… Press Ctrl+C to stop. “, end=”, flush=True)
                # Small sleep to prevent the CPU from running at 100%
                time.sleep(0.5)
    except KeyboardInterrupt:
        # Allows the user to exit gracefully using Ctrl+C
        print(“\nTrace terminated.”)
if __name__ == “__main__”:
    monitor_ethernet()
Posted on March 20, 2026, 4:09 pm By
Categories: tech tips