如何检测网线/连接器的物理连接状态? [英] How to detect the physical connected state of a network cable/connector?

查看:36
本文介绍了如何检测网线/连接器的物理连接状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Linux 环境中,我需要检测 RJ45 连接器与其插座的物理连接或断开状态.最好只使用 BASH 脚本.

在其他网站上提出的以下解决方案不适用于此目的:

  1. 使用ifconfig" - 因为网络电缆可能已连接但网络未正确配置或当前未启动.
  2. Ping 主机 - 因为产品将位于使用未知网络配置和未知主机的 LAN 中.

在/proc 文件系统中没有可以使用的状态吗(其他所有东西都在那里)?

Linux 世界是如何假设自己版本的 Windows 气泡从图标托盘中弹出的,表明您刚刚拔掉了网线?

<小时>

Kent Fredriclothar,你们的回答都满足了我的需要……非常感谢!我会用哪一个……我还是不知道.

我想我不能把你们俩都当作正确答案?我选择一个对你来说可能是公平的.我猜是抛硬币吗?再次感谢!

解决方案

你想看看

中的节点<前>/系统/类/网/

我用我的做了实验:

电线插入:

eth0/carrier:1eth0/操作状态:未知

电线移除:

eth0/carrier:0eth0/operstate:down

再次插入电线:

eth0/carrier:1eth0/操作状态:向上

小窍门:用简单的方法一次性收集所有属性:

grep "" eth0/*

这形成了一个很好的 key:value 对列表.

In a Linux environment, I need to detect the physical connected or disconnected state of an RJ45 connector to its socket. Preferably using BASH scripting only.

The following solutions which have been proposed on other sites do NOT work for this purpose:

  1. Using 'ifconfig' - since a network cable may be connected but the network not properly configured or not currently up.
  2. Ping a host - since the product will be within a LAN using an unknown network configuration and unknown hosts.

Isn't there some state which can be used in the /proc file system (everything else is in there)?

How is the Linux world suppose to have their own version of the Windows bubble that pop up from the icon tray indicating that you've just unplugged the network cable?


Kent Fredric and lothar, both of your answers satisfy my need... thanks a lot! Which one I'll use... I still don't know.

I guess I can't put you both down as the correct answer? And its probably fair for you that I do choose one. Flip a coin I guess? Again, thanks!

解决方案

You want to look at the nodes in

/sys/class/net/

I experimented with mine:

Wire Plugged in:

eth0/carrier:1
eth0/operstate:unknown

Wire Removed:

eth0/carrier:0
eth0/operstate:down

Wire Plugged in Again:

eth0/carrier:1
eth0/operstate:up

Side Trick: harvesting all properties at once the easy way:

grep "" eth0/* 

This forms a nice list of key:value pairs.

这篇关于如何检测网线/连接器的物理连接状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆