Script to check if network cable connected to all interfaces in Linux ?

In this post, we will check a small command to check network cable status on all eth or network interfaces of our Linux system.

Ideally we used to check for individual interfaces using ethtool command like below:

[root@nglinux ~]# ethtool eth2
Settings for eth2:
	Supported ports: [ TP ]
	Supported link modes:   1000baseT/Full 
	Supported pause frame use: No
	Supports auto-negotiation: No
	Advertised link modes:  Not reported
	Advertised pause frame use: No
	Advertised auto-negotiation: No
	Speed: 1000Mb/s
	Duplex: Full
	Port: Twisted Pair
	Transceiver: internal
	Auto-negotiation: off
	MDI-X: Unknown
	Link detected: yes
[root@nglinux ~]# 

However suppose we have 10 interfaces, and we want to check link status on all 10 devices in one go, so we can use a command like below:

[root@nglinux ~]# for i in `ls /sys/class/net/`; do ethtool $i | sed -n '1p;$p'| tr "\n" "\t"   ;echo ; done
Settings for eth2:		Link detected: yes	
Settings for lo:		Link detected: yes	
[root@nglinux ~]#	

You can save above command in your notes, and whenever you need to look the output for all interfaces on your server, you can use above command.

The above command simply runs ethtool on all interfaces and then greps the Link detected line.

Please do post your comments below.

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments