How to detect the physical connected state of a network cable/connector?
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!
You want to look at the nodes in
/sys/class/net/
I experimented with mine:
Wire Plugged in:
eth0/carrier:1
eth0/operstate:unknown
Wire Removed:
eth0/carrier:0
eth0/operstate:down
Wire Plugged in Again:
eth0/carrier:1
eth0/operstate:up
Side Trick: harvesting all properties at once the easy way:
grep "" eth0/*
This forms a nice list of key:value
pairs.
You can use ethtool:
$ sudo ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
1000baseT/Full
Advertised auto-negotiation: Yes
Speed: 1000Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 0
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: umbg
Wake-on: g
Current message level: 0x00000007 (7)
Link detected: yes
To only get the Link status you can use grep:
$ sudo ethtool eth0 | grep Link
Link detected: yes