【问题标题】:How to find physical interfaces on linux (without bridges, aliases, vlans ..)如何在 linux 上找到物理接口(没有网桥、别名、vlan ..)
【发布时间】:2016-05-02 06:36:15
【问题描述】:

我需要识别 (Debian) Linux 上的物理接口 如果显示到 /sys/class/net 我会看到所有接口,包括网桥和 vlan。

或者可以使用 bash 来检查一个接口是否存在以及它是物理的还是虚拟的?

【问题讨论】:

    标签: linux bash networking


    【解决方案1】:

    检查位于/sys/class/net/<interface>/uevent 中的uevent 文件中的DEVTYPE 参数。

    在我的桥接界面中:

    $ cat /sys/class/net/br0/uevent 
    DEVTYPE=bridge
    INTERFACE=br0
    IFINDEX=3
    

    虽然创建网桥的真实物理接口没有参数:

    $ cat /sys/class/net/eth0/uevent
    INTERFACE=eth0
    IFINDEX=2
    

    【讨论】:

    • 感谢您的想法;)物理接口在 /sys/class/net// 文件夹中有一个“设备”符号链接。虚拟设备不
    【解决方案2】:

    一种方法是匹配/sys/class/net/中的符号链接路径:

    $ ls -la /sys/class/net/
    
    lrwxrwxrwx  1 root root 0 18. 5. 04:51 docker0 -> ../../devices/virtual/net/docker0
    lrwxrwxrwx  1 root root 0 18. 5. 04:51 eno1 -> ../../devices/pci0000:00/0000:00:19.0/net/eno1
    lrwxrwxrwx  1 root root 0 18. 5. 04:51 enp9s0 -> ../../devices/pci0000:00/0000:00:1c.5/0000:09:00.0/net/enp9s0
    lrwxrwxrwx  1 root root 0 18. 5. 04:51 lo -> ../../devices/virtual/net/lo
    lrwxrwxrwx  1 root root 0 18. 5. 19:17 macvtap0 -> ../../devices/virtual/net/macvtap0
    

    /devices/virtual/net/ 路径适用于所有虚拟接口。

    因此您可以使用find 来匹配所有包含*/devices/virtual/net/*的链接:

    $ find /sys/class/net/ -type l ! -lname '*/devices/virtual/net/*'
    /sys/class/net/eno1
    /sys/class/net/enp9s0
    

    这可以转成脚本:

    #!/bin/bash
    
    for iface in $(find /sys/class/net/ -type l ! -lname '*/devices/virtual/net/*' -printf '%f '); 
    do
      echo "$iface is not virtual"
    done 
    

    示例输出:

    # ./ifs.sh 
    eno1 is not virtual
    enp9s0 is not virtual
    

    【讨论】:

      【解决方案3】:

      两种可能的解决方案

      1. /sys/class/net/< interface >/文件夹中显示device符号链接,它只出现在物理接口上

      2. ip -d show link可以找到不同类型的接口

        if  [[ ! `ip -d link show ${int_name}  2>/dev/null >/dev/null` ]]; then
            echo "Interface ${int_name} does not exists"
        elif [[ `ip -d link show ${int_name} | tail -n +2 | grep loopback` ]] ; then
            echo is_local
        elif [[ `ip -d link show ${int_name} | tail -n +2 | grep vlan` ]] ; then
            echo is_vlan
        elif [[ `ip -d link show ${int_name} | tail -n +2 | grep bridge` ]] ; then
            echo is_bridge
        else
            echo is_physic
        fi
        

      【讨论】:

        【解决方案4】:

        使用 systemd 的更好解决方案(带有 vlan/bridges 的树莓派示例)

        systemctl -a | grep net-devices | grep -v "/sys/subsystem"
        

        【讨论】:

          【解决方案5】:

          扩展解决方案,适用于 pi 和其他设备 linke tinker board/rock64

          systemctl -a | grep sys-devices-platform | grep '\-net-' | awk '{n=split($1,A,"-"); print A[n]}' | cut -d"." -f1
          

          【讨论】:

            猜你喜欢
            • 2017-03-21
            • 2018-02-26
            • 2017-10-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-22
            • 2017-11-03
            相关资源
            最近更新 更多