Linux检测网线拔出:即网线线缆是否断开。判断网线是否断开方法如下:
方法1:linux系统终端输入命令行: cat /sys/class/net/eth0/carrier,即可确认网线是否拔出。
方法2:在应用层程序里面,读取carrier中的值 即可:
skfd = open("/sys/class/net/eth0/carrier", O_RDONLY) ;
if(skfd < 0)
printf("cat eth0 error!\n") ;
else
printf("cat eth0 success!\n") ;
具体说明如下:
/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/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 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