2

I want that my program prints a statement whenever there is a physical disconnection of the LAN connection from my Ethernet port.

One way is to check if there is a discontinuity in the internet connection,but i don't want the print statement to be triggered when there is a network issue from the server.Only when the wire is physically not connected to my PC.

Is there any way to implement this in Python?

Rhea
  • 21
  • 1
  • 3
  • 1
    Any more details? Why cannot you use standard way of doing this? Have you tried any way, and why it doesn't suit your needs? For example, on network interruption running kernel command to list devices and checking if device you are using is running. – Tomasz Plaskota Jul 17 '17 at 13:49

2 Answers2

1

I think there is already an answer to this question: https://stackoverflow.com/a/808599

EDIT: To brief it shortly, you can use the properties in /sys/class/net/DEVICE/* to check if the wire is connected, if you run Linux. You can just access these files using the default python methods.

For Windows, there is no way (or probably a very ugly way) to check that in Python.

caylee
  • 921
  • 6
  • 12
-1

You can use this code snippet:

def is_interface_up(interface):
    addr = netifaces.ifaddresses(interface)
    return netifaces.AF_INET in addr

It will return True if the interface is up. You can check is_interface_up periodically and print if the returned value changes.

maestromusica
  • 706
  • 8
  • 23