如何检测网络电缆/连接器的物理连接状态?

Jea*_*ach 138 linux connection networking detection

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

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

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

是不是有一些状态可以在/ proc文件系统中使用(其他一切都在那里)?

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


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

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

Ken*_*ric 214

你想看看中的节点

/sys/class/net/

我试验过我的:

电线插入:

eth0/carrier:1
eth0/operstate:unknown
Run Code Online (Sandbox Code Playgroud)

电线已删除:

eth0/carrier:0
eth0/operstate:down
Run Code Online (Sandbox Code Playgroud)

电线又插入:

eth0/carrier:1
eth0/operstate:up
Run Code Online (Sandbox Code Playgroud)

Side Trick:轻松获取所有属性:

grep "" eth0/* 
Run Code Online (Sandbox Code Playgroud)

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

  • grep""eth0/*非常优雅和简单,谢谢!:)使用-s开关grep不会抱怨目录. (10认同)
  • 请注意,正如Marco所说,接口必须启动(即使未配置)以查询这些值. (7认同)
  • 我更喜欢:`grep -H.eth0/*`:这个whipe emty行和打印条目名称属于每一行. (2认同)
  • 可以使用以下命令忽略 grep 中有关目录的错误:`grep -s "" eth0/*` (2认同)

lot*_*har 82

你可以使用ethtool:

$ sudo ethtool eth0
Settings for eth0:
    Supported ports: [ TP ]
    Supported link modes:   10baseT/Half 10baseT/Full
                            100baseT/Half 100baseT/Full
                            1000baseT/Full
    Supports auto-negotiation: Yes
    Advertised link modes:  10baseT/Half 10baseT/Full
                            100baseT/Half 100baseT/Full
                            1000baseT/Full
    Advertised auto-negotiation: Yes
    Speed: 1000Mb/s
    Duplex: Full
    Port: Twisted Pair
    PHYAD: 0
    Transceiver: internal
    Auto-negotiation: on
    Supports Wake-on: umbg
    Wake-on: g
    Current message level: 0x00000007 (7)
    Link detected: yes
Run Code Online (Sandbox Code Playgroud)

要仅获取链接状态,您可以使用grep:

$ sudo ethtool eth0 | grep Link
    Link detected: yes
Run Code Online (Sandbox Code Playgroud)

  • 请注意,正如Marco所说,接口必须启动(即使未配置)以查询这些值. (3认同)

Pet*_*ing 26

使用'ip monitor'获取REAL TIME链接状态更改.

  • 在我的情况下,这是唯一有效的答案.../sys/class/net/eth0/carrier当我的电缆断开时仍显示`1`而`ip monitor`实际显示的东西 (3认同)

小智 17

cat /sys/class/net/ethX 是迄今为止最简单的方法.

接口必须是up,否则你将得到一个无效的参数错误.

首先:

ifconfig ethX up
Run Code Online (Sandbox Code Playgroud)

然后:

cat /sys/class/net/ethX
Run Code Online (Sandbox Code Playgroud)

  • 尝试"cat/sys/class/net/eth [n]/operstate",其中[n]是eth设备号. (4认同)

Amb*_*jak 8

在较低级别,可以使用rtnetlink套接字捕获这些事件,而不进行任何轮询.旁注:如果使用rtnetlink,则必须与udev一起使用,否则当udev重命名新的网络接口时,您的程序可能会感到困惑.

使用shell脚本进行网络配置的问题在于,shell脚本对于事件处理非常糟糕(例如插入和拔出网络电缆).如果您需要更强大的功能,请查看我的NCD编程语言,这是一种专为网络配置而设计的编程语言.

例如,一个简单的NCD脚本将打印"cable in"和"cable out"到stdout(假设接口已经启动):

process foo {
    # Wait for device to appear and be configured by udev.
    net.backend.waitdevice("eth0");
    # Wait for cable to be plugged in.
    net.backend.waitlink("eth0");
    # Print "cable in" when we reach this point, and "cable out"
    # when we regress.
    println("cable in");   # or pop_bubble("Network cable in.");
    rprintln("cable out"); # or rpop_bubble("Network cable out!");
                           # just joking, there's no pop_bubble() in NCD yet :)
}
Run Code Online (Sandbox Code Playgroud)

(内部net.backend.waitlink()使用rtnetlink,并net.backend.waitdevice()使用udev)

NCD的想法是你专门用它来配置网络,所以通常,配置命令介于两者之间,例如:

process foo {
    # Wait for device to appear and be configured by udev.
    net.backend.waitdevice("eth0");
    # Set device up.
    net.up("eth0");
    # Wait for cable to be plugged in.
    net.backend.waitlink("eth0");
    # Add IP address to device.
    net.ipv4.addr("eth0", "192.168.1.61", "24");
}
Run Code Online (Sandbox Code Playgroud)

需要注意的重要部分是允许执行倒退 ; 例如,在第二个示例中,如果拉出电缆,IP地址将自动删除.


Sar*_*oev 6

我使用此命令来检查电线是否已连接:

cd /sys/class/net/
grep "" eth0/operstate
Run Code Online (Sandbox Code Playgroud)

如果结果会上升或下降。有时显示未知,则需要检查

eth0/carrier
Run Code Online (Sandbox Code Playgroud)

显示0或1


ger*_*ber 5

存在两个检测这些事件的守护进程:

ifplugdnetplugd