In a Linux environment, I need to detect the physical connected or disconnected state of an RJ45 connector to its socket. Preferably using BASH scripting only.
The following solutions which have been proposed on other sites do NOT work for this purpose:
- Using 'ifconfig' - since a network cable may be connected but the network not properly configured or not currently up.
- Ping a host - since the product will be within a LAN using an unknown network configuration and unknown hosts.
Isn't there some state which can be used in the /proc file system (everything else is in there)?
How is the Linux world suppose to have their own version of the Windows bubble that pop up from the icon tray indicating that you've just unplugged the network cable?
Kent Fredric and lothar, both of your answers satisfy my need... thanks a lot! Which one I'll use... I still don't know.
I guess I can't put you both down as the correct answer? And its probably fair for you that I do choose one. Flip a coin I guess? Again, thanks!
cat /sys/class/net/ethX
is by far the easiest method.The interface has to be up though, else you will get an invalid argument error.
So first:
Then:
Use 'ip monitor' to get REAL TIME link state changes.
You can use ethtool:
To only get the Link status you can use grep:
There exists two daemons that detect these events:
ifplugd and netplugd
Most modern Linux distributions use NetworkManager for this. You could use D-BUS to listen for the events.
If you want a command-line tool to check the status, you can also use
mii-tool
, given that you have Ethernet in mind.