Tri*_*yas 7 command-line networking ethernet network-interface
如您所知,在 Windows 中,当我们插入网线时,网络符号会变为另一种状态。
如何通过 Linux 中的命令提示符知道电缆是否已插入?
slm*_*slm 17
我见过的主要使用的 2 种方法是使用ethtool或手动解析/sys.
例如,如果您的界面是,eth0您可以使用它进行查询ethtool,然后解析“检测到链接”这一行。
$ sudo ethtool eth0
Settings for eth0:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Supports auto-negotiation: Yes
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Advertised auto-negotiation: Yes
Speed: 100Mb/s
Duplex: Full
Port: Twisted Pair
PHYAD: 1
Transceiver: internal
Auto-negotiation: on
Supports Wake-on: pumbag
Wake-on: g
Current message level: 0x00000001 (1)
Link detected: yes
Run Code Online (Sandbox Code Playgroud)
具体这个命令:
$ ethtool eth0 | grep "Link"
Link detected: yes
Run Code Online (Sandbox Code Playgroud)
如果它下降,它会说不。
再次假设我们对 感兴趣eth0,您可以手动解析 的内容,/sys/class/net/然后eth0为您的设备解析。这个目录下有 2 个文件会告诉你链接是否处于活动状态,carrier以及operstate:
连接电线后,这两个文件将显示如下:
$ cat /sys/class/net/eth0/{carrier,operstate}
1
up
Run Code Online (Sandbox Code Playgroud)
当电线断开时,这两个文件将显示如下:
$ cat /sys/class/net/eth0/{carrier,operstate}
0
down
Run Code Online (Sandbox Code Playgroud)
# mii-tool em1
em1: negotiated 100baseTx-FD flow-control, link ok
Run Code Online (Sandbox Code Playgroud)
还有来自 NetworkManager 的nmcli:
$ nmcli -f capabilities.carrier-detect,capabilities.speed device show em1
CAPABILITIES.CARRIER-DETECT: yes
CAPABILITIES.SPEED: 100 Mb/s
Run Code Online (Sandbox Code Playgroud)
*device可以缩短为d
| 归档时间: |
|
| 查看次数: |
19700 次 |
| 最近记录: |