/sys/class/net/eth0/carrier


https://linuxconfig.org/how-to-detect-whether-a-physical-cable-is-connected-to-network-card-slot-on-linux

 

 /sys/class/net  里面顯示了所有的網卡,但不是所有的都處於激活狀態

 

Various tools can be used to detected a physical cable carrier state. However, the easiest accomplish this task is by using basic native tools like cat or grep thus to avoid any need to for additional software installation. Let's start by testing our eth0 network interface for a physical cable connection in a low-level and Linux distro-agnostic way:

# 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. Next, we will test second network interface eth1:

~# cat /sys/class/net/eth1/carrier 
cat: /sys/class/net/eth1/carrier: Invalid argument
對於未激活的網卡,ioctl(sk, SIOCGIFADDR, &ifr)返回-1,失敗,是取不到該網卡的ip的

The above command's output most likely means the the eth1 network interface is in powered down state. This can be confirmed by the following command:

# cat /sys/class/net/eth1/operstate 
down

The network cable can be connected but there is no way to tell at the moment. Before we can check for a physical cable connection we need to turn it up:

# ifconfig eth1 up

At this stage we can again check for a network card physical cable connection:

# cat /sys/class/net/eth1/carrier 
0

Based on the above output we can say that a physical cable is disconnected from the network card's slot. Let's let's see briefly how we can automate the above procedure to check multiple network interfaces at once. The below command will list all available network interfaces on your Linux system:

# for i in $( ls /sys/class/net ); do echo $i; done
eth0
eth1
lo
wlan0

Using a bash for loop we can now check whether a network cable is connected for all network interfaces at once:

# for i in $( ls /sys/class/net ); do echo -n $i: ; cat /sys/class/net/$i/carrier; done
eth0:1
eth1:0
lo:1
wlan0:cat: /sys/class/net/wlan0/carrier: Invalid argument

1. Test for physical cable connection with ethtool

Now, if you really want to get fancy you can do the above task using ethtool command. Installation:

REDHAT/CENTOS/FEDORA/SUSE
# yum install ethtool
DEBIAN/UBUNTU
# apt-get install ethtool

To check a single network card for a cable connection use. For an example, let's check eth1:

#  ethtool eth1 | grep Link\ d
	Link detected: no

Or 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
eth0	Link detected: yes
eth1	Link detected: no
lo	Link detected: yes
wlan0	Link detected: no

NOTE:
The only problem with the above ethtooloutput 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM