How to determine if the Ethernet cable is unplugged in Linux.
If the network interface name is eth0
cat /sys/class/net/eth0/carrier
It can be judged by the value displayed in. 0 is the disconnection state. 1 is live. Therefore, if you monitor this, you can easily determine whether to connect or disconnect the cable.
However, inotify cannot be used with sysfs or procfs. So if you decide by yourself https://stackoverflow.com/questions/26672414/inotify-add-watch-fails-on-sys-class-net-eth0-operstate You need to get the event using netlink, as in.
However, if there are multiple network interfaces, I do not know which interface the LAN cable was disconnected from. In this case, it is necessary to determine by combining with the value of / sys / class / net / eth0 / carrier mentioned above.
I thought there might be a better way, but I couldn't find a way to say this.
For the time being, I decided to make the tea muddy by this method.
Recommended Posts