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

后端 未结 15 1339
隐瞒了意图╮
隐瞒了意图╮ 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:01

    On OpenWRT the only way to reliably do this, at least for me, is by running these commands:

    # Get switch name
    swconfig list
    
    # assuming switch name is "switch0"
    swconfig dev switch0 show | grep link:
    
    # Possible output
    root@OpenWrt:~# swconfig dev switch0 show | grep link:
            link: port:0 link:up speed:1000baseT full-duplex txflow rxflow
            link: port:1 link:up speed:1000baseT full-duplex txflow rxflow eee100 eee1000 auto
            link: port:2 link:up speed:1000baseT full-duplex txflow rxflow eee100 eee1000 auto
            link: port:3 link:down
            link: port:4 link:up speed:1000baseT full-duplex eee100 eee1000 auto
            link: port:5 link:down
            link: port:6 link:up speed:1000baseT full-duplex txflow rxflow
    

    This will show either "link:down" or "link:up" on every port of your switch.

    0 讨论(0)
  • 2020-12-04 05:06

    Use 'ip monitor' to get REAL TIME link state changes.

    0 讨论(0)
  • 2020-12-04 05:06

    I was using my OpenWRT enhanced device as a repeater (which adds virtual ethernet and wireless lan capabilities) and found that the /sys/class/net/eth0 carrier and opstate values were unreliable. I played around with /sys/class/net/eth0.1 and /sys/class/net/eth0.2 as well with (at least to my finding) no reliable way to detect that something was physically plugged in and talking on any of the ethernet ports. I figured out a bit crude but seemingly reliable way to detect if anything had been plugged in since the last reboot/poweron state at least (which worked exactly as I needed it to in my case).

    ifconfig eth0 | grep -o 'RX packets:[0-9]*' | grep -o '[0-9]*'
    

    You'll get a 0 if nothing has been plugged in and something > 0 if anything has been plugged in (even if it was plugged in and since removed) since the last power on or reboot cycle.

    Hope this helps somebody out at least!

    0 讨论(0)
  • 2020-12-04 05:07
    tail -f /var/log/syslog | grep -E 'link (up|down)'
    

    or for me faster gets:

    tail -f /var/log/syslog | grep 'link \(up\|down\)'
    

    It will listen to the syslog file.

    Result (if disconnect and after 4 seconds connect again):

    Jan 31 13:21:09 user kernel: [19343.897157] r8169 0000:06:00.0 enp6s0: link down
    Jan 31 13:21:13 user kernel: [19347.143506] r8169 0000:06:00.0 enp6s0: link up
    
    0 讨论(0)
  • 2020-12-04 05:09

    Most modern Linux distributions use NetworkManager for this. You could use D-BUS to listen for the events.

    If you want a command-line tool to check the status, you can also use mii-tool, given that you have Ethernet in mind.

    0 讨论(0)
  • 2020-12-04 05:09

    on arch linux. (im not sure on other distros) you can view the operstate. which shows up if connected or down if not the operstate lives on

    /sys/class/net/(interface name here)/operstate
    #you can also put watch 
    watch -d -n -1 /sys/class/net/(interface name here)/operstate
    
    0 讨论(0)
提交回复
热议问题