{"id":536,"date":"2026-03-20T16:09:55","date_gmt":"2026-03-20T16:09:55","guid":{"rendered":"https:\/\/xuan.co.uk\/?p=536"},"modified":"2026-03-20T16:11:53","modified_gmt":"2026-03-20T16:11:53","slug":"ethernet-up-lan-alert","status":"publish","type":"post","link":"https:\/\/xuan.co.uk\/?p=536","title":{"rendered":"Ethernet up \/ Lan Alert"},"content":{"rendered":"<p>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.<\/p>\n<p>Install phyton, pip install psutil.<\/p>\n<p>&nbsp;<\/p>\n<div>\n<div>import psutil\u00a0 \u00a0# Retrieves hardware status (network interfaces)<\/div>\n<div>import time\u00a0 \u00a0 \u00a0# Manages timing and delays<\/div>\n<div>import platform # Detects if the system is Windows, Linux, or macOS<\/div>\n<div><\/div>\n<div>def play_alert():<\/div>\n<div>\u00a0 \u00a0 &#8220;&#8221;&#8221;Triggers a high-pitched audible alert.&#8221;&#8221;&#8221;<\/div>\n<div>\u00a0 \u00a0 if platform.system() == &#8216;Windows&#8217;:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 import winsound<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 # Frequency: 2500Hz (High pitch), Duration: 500ms (Half second)<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 winsound.Beep(2500, 500)<\/div>\n<div>\u00a0 \u00a0 else:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 # Fallback for non-Windows systems using the terminal bell<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 print(&#8216;\\a&#8217;, end=&#8221;, flush=True)<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 time.sleep(0.5)<\/div>\n<div><\/div>\n<div>def monitor_ethernet():<\/div>\n<div>\u00a0 \u00a0 &#8220;&#8221;&#8221;Main loop to detect physical Ethernet link status.&#8221;&#8221;&#8221;<\/div>\n<div>\u00a0 \u00a0 # Get the initial list of network interfaces<\/div>\n<div>\u00a0 \u00a0 stats = psutil.net_if_stats()<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 # Filter for physical Ethernet ports (ignores Wi-Fi\/Virtual adapters)<\/div>\n<div>\u00a0 \u00a0 # Common Ethernet naming: &#8216;Ethernet&#8217;, &#8216;eth0&#8217;, &#8216;enp0s3&#8217;, &#8216;en0&#8217;<\/div>\n<div>\u00a0 \u00a0 eth_interfaces = [<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 iface for iface in stats.keys()<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 if &#8216;eth&#8217; in iface.lower() or &#8216;ethernet&#8217; in iface.lower() or &#8216;en&#8217; in iface.lower()<\/div>\n<div>\u00a0 \u00a0 ]<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 if not eth_interfaces:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 print(&#8220;No Ethernet interfaces detected. Check your Device Manager.&#8221;)<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 return<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 print(f&#8221;Monitoring interfaces: {&#8216;, &#8216;.join(eth_interfaces)}&#8221;)<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 try:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 while True:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # Refresh the interface stats in each loop<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 current_stats = psutil.net_if_stats()<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # Check if any identified Ethernet port has &#8216;isup&#8217; as True<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # &#8216;isup&#8217; becomes True when a physical electrical link is established<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 is_any_up = any(<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 current_stats[iface].isup<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 for iface in eth_interfaces if iface in current_stats<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 )<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if is_any_up:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # \\r keeps the output on one line to avoid scrolling<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(&#8220;\\r[!] LIVE LINK DETECTED &#8211; Alert sounding&#8230;\u00a0 \u00a0 \u00a0&#8220;, end=&#8221;, flush=True)<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 play_alert()<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 else:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(&#8220;\\rAwaiting connection&#8230; Press Ctrl+C to stop. &#8220;, end=&#8221;, flush=True)<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # Small sleep to prevent the CPU from running at 100%<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 time.sleep(0.5)<\/div>\n<div><\/div>\n<div>\u00a0 \u00a0 except KeyboardInterrupt:<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 # Allows the user to exit gracefully using Ctrl+C<\/div>\n<div>\u00a0 \u00a0 \u00a0 \u00a0 print(&#8220;\\nTrace terminated.&#8221;)<\/div>\n<div><\/div>\n<div>if __name__ == &#8220;__main__&#8221;:<\/div>\n<div>\u00a0 \u00a0 monitor_ethernet()<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>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. &nbsp; import psutil\u00a0 \u00a0# Retrieves hardware status (network interfaces) import time\u00a0 \u00a0 \u00a0# <a class=\"more-link\" href=\"https:\/\/xuan.co.uk\/?p=536\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-536","post","type-post","status-publish","format-standard","hentry","category-tech-tips"],"_links":{"self":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=536"}],"version-history":[{"count":2,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/536\/revisions"}],"predecessor-version":[{"id":538,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/536\/revisions\/538"}],"wp:attachment":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}