How to detect the physical connected state of a network cable/connector?

后端 未结 15 1369
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 04:23

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

15条回答
  •  無奈伤痛
    2020-12-04 05:16

    You can use 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
    

    To only get the Link status you can use grep:

    $ sudo ethtool eth0 | grep Link
        Link detected: yes
    

提交回复
热议问题