Friday, March 16, 2018

how to check physical network cable connection status on linux ?

Method 1

Using dmesg

Using dmesg is one of the 1st things to do for inquiring current state of system:

Example:

dmesg | sed '/eth.*Link is/h;${x;p};d'
[1667676.292871] e1000e: eth0 NIC Link is Up 100 Mbps Full Duplex, Flow Control: Rx/Tx

Method 2

/sys/class/net/

cat /sys/class/net/eth0/carrier
1

The number 1 in the above output means that the network cable is connection physically
to your's network card slot.

Or

cat /sys/class/net/eth0/operstate
up


Method 3

Using ethtool command

Syantax : ethtool interface_name | grep Link\ d

Example:

ethtool eth0 | grep Link\ d

Link detected: yes

we can use bash for loop again to check all network interfaces it once:

for i in $( ls /sys/class/net ); do echo -n $i; ethtool $i | grep Link\ d; done

Sample output:

eth0 Link detected: yes
eth1 Link detected: no
lo Link detected: yes
wlan0 Link detected: no

NOTE:

The only problem with the above ethtool output is that it will not detect connected
cable if your network interface is down. Consider a following example:
# ethtool eth0 | grep Link\ d
       Link detected: yes
# ifconfig eth0 down
# ethtool eth0 | grep Link\ d
       Link detected: no
# ifconfig eth0 up
# ethtool eth0 | grep Link\ d
       Link detected: yes