判断嵌入式设备或linux系统网络物理上是否连接

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 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

 

本文参考网址:https://linuxconfig.org/how-to-detect-whether-a-physical-cable-is-connected-to-network-card-slot-on-linux

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值