Is it possible to have a script running as a service constantly monitor certain NICs and alert me throught LEDs if one of them has been disconnected or is not properly detected by the OS?

Would running something like this use up a lot of the Pi's resources?

I am aware of tutorials on using the Pi as a network monitor. What I would like is to have a script that constantly monitors whether or not certain interfaces are connected and to be alerted when one of them is disconnected.

  • Which interfaces - like the Ethernet port/ Wi-Fi? A simple looping script to parse ifconfig and then output GPIO should then suffice (Python, bash etc). – trishmapow Dec 29 '18 at 5:01
  • "not properly detected" - if a NIC is not detected how do you know it's there? – Jaromanda X Dec 29 '18 at 5:03
  • Hi. Thanks for answering. Yes, I mean for these interfaces. I think this should be doable, but won't this use up a lot of cpu or ram especially if I'm constantly running this? Is there a more efficient way of doing it? – user942937 Dec 29 '18 at 5:05
  • there's probably a system event or something you can hook into when an interface goes up or down - sounds like you want to Get notified about network interface change on Linux – Jaromanda X Dec 29 '18 at 5:07
  • @Jaromanda X: If the USB port the interface is connected to fails or doesn't receive enough power or if the interface gets an error like "device not accepting" address, problems that might occur during startup or when I plug them in. – user942937 Dec 29 '18 at 5:09

On Linux systems, there's a special file at /sys/class/net/$interface/carrier (where $interface is your interface name, e.g., eth0, wlan0). You can read from it like you would any other file in Python. If you read a 1, the interface is connected, and if you read a 0, it is not connected.

Here's some untested example code to illustrate:

def isConnected(interface):
    """Example usage: isConnected("eth0")
    Returns True if connected, False if not"""
    with open("/sys/class/net/{}/carrier".format(interface)) as f:
        contents = f.read()
        if contents == "0":
            return False
        elif contents == "1":
            return True

You should be able to incorporate this into your own script to trigger LEDs to go on or off based on the connection status.

| improve this answer | |
  • Hi. Thanks for answering. Will the open statement open the file once and monitor any changes? Will I have to run this inside a loop or can I put it inside a script that will run as a service/daemon? – user942937 Jan 4 '19 at 7:39
  • The open statement just opens the file once for reading, so you would have to put it inside a loop. (You might want to look into a better way to monitor a file for changes in Python). You would also need to add your GPIO code to toggle the LEDs. You could then run the script at startup using /etc/rc.local (you can find tutorials for this online). – Breq Jan 4 '19 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.