2012/04/18

Linux下如何偵測網路介面是否有插上線

如果一台主機上有8個 network interface (eth0 - eth7),要怎麼知道我現在插上的網路線是對應到ethN?


$ 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

Link detected: yes 表示是有插上線的,但前提是一定要先把 interface 叫起來 (ifconfig eth0 up),這個偵測才有用。


參考資料中還有提到其他的方法
$ grep "" /sys/class/net/eth0/*
...
/sys/class/net/eth0/carrier:1
...
/sys/class/net/eth0/operstate:up
...


參考資料
linux - How to detect the physical connected state of a network cable/connector? - Stack Overflow


沒有留言:

張貼留言