如何计算网卡数量?

如何计算网卡数量?

如何计算插入的网卡数量?

预期最终结果(如):

server0 ; 1
server1 ; 2
server2 ; 1
server3 ; 2

我到目前为止尝试过的:比较字符串并对其进行计数:

# lspci -mm

(1)这些卡的一些控制器(即使它们属于同一张卡)对设备名称有不同的输出

(2) 尽管每个控制器位于同一张卡上,但它们都有不同的名称并接收不同的“pci 地址”。

现在我正在寻找另一种方法。(解析大量输出不会有问题。我接受任何可行的想法:)

编辑:现在我的问题已经解决,我想分享一些我在这个主题上发现的更多选项。

  1. 将接口的总线地址(来自 lspci)与 dmidecode PCI 和以太网进行比较,您可以看到它是集成的还是额外连接的 NIC(我遇到的一个问题:Bus Address: 0000:3b:00.0dmidecode 没有提供每个接口的子地址,但至少您可以看到有一张卡!)

  2. omreport chassis nics如果您的服务器来自 DELL 并且配置了 OMSA,则您可以使用 omreport,它具有大量可供您使用的详细信息。

样品:

(1)

Server0 # lspci -mm | grep Eth
18:00.0 "Ethernet controller" "Intel Corporation" "Ethernet Controller 10G X550T" -r01 "Dell" "Ethernet 10G 4P X550/I350 rNDC"
18:00.1 "Ethernet controller" "Intel Corporation" "Ethernet Controller 10G X550T" -r01 "Dell" "Ethernet 10G 4P X550/I350 rNDC"
19:00.0 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Dell" "Gigabit 4P X550/I350 rNDC"
19:00.1 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Dell" "Gigabit 4P X550/I350 rNDC"

(2)

Server1 # lspci -mm | grep Eth
01:00.0 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Broadcom Inc. and subsidiaries" "Device 4160"
01:00.1 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Broadcom Inc. and subsidiaries" "Device 4160"
19:00.0 "Ethernet controller" "Broadcom Inc. and subsidiaries" "BCM57416 NetXtreme-E Dual-Media 10G RDMA Ethernet Controller" -r01 "Broadcom Inc. and subsidiaries" "Device 4160"
19:00.1 "Ethernet controller" "Broadcom Inc. and subsidiaries" "BCM57416 NetXtreme-E Dual-Media 10G RDMA Ethernet Controller" -r01 "Broadcom Inc. and subsidiaries" "Device 4160"
3b:00.0 "Ethernet controller" "Broadcom Inc. and subsidiaries" "BCM57416 NetXtreme-E Dual-Media 10G RDMA Ethernet Controller" -r01 "Broadcom Inc. and subsidiaries" "Device 4161"
3b:00.1 "Ethernet controller" "Broadcom Inc. and subsidiaries" "BCM57416 NetXtreme-E Dual-Media 10G RDMA Ethernet Controller" -r01 "Broadcom Inc. and subsidiaries" "Device 4161"


Server2 # lspci -mm | grep Eth
01:00.0 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Broadcom Inc. and subsidiaries" "Device 4160"
01:00.1 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Broadcom Inc. and subsidiaries" "Device 4160"
18:00.0 "Ethernet controller" "Broadcom Inc. and subsidiaries" "BCM57416 NetXtreme-E Dual-Media 10G RDMA Ethernet Controller" -r01 "Broadcom Inc. and subsidiaries" "Device 4160"
18:00.1 "Ethernet controller" "Broadcom Inc. and subsidiaries" "BCM57416 NetXtreme-E Dual-Media 10G RDMA Ethernet Controller" -r01 "Broadcom Inc. and subsidiaries" "Device 4160"


Server3 # lspci -mm | grep Eth
02:00.0 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Dell" "Device 0639"
02:00.1 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Dell" "Device 0639"
03:00.0 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Dell" "Device 0639"
03:00.1 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Dell" "Device 0639"
05:00.0 "Ethernet controller" "Intel Corporation" "Ethernet Controller 10-Gigabit X540-AT2" -r01 "Intel Corporation" "Ethernet 10G 2P X540-t Adapter"
05:00.1 "Ethernet controller" "Intel Corporation" "Ethernet Controller 10-Gigabit X540-AT2" -r01 "Intel Corporation" "Ethernet 10G 2P X540-t Adapter"

DMIDE代码:

Handle 0x0900, DMI type 9, 17 bytes
System Slot Information
        Designation: PCIe Slot 1
        Type: x8 PCI Express 3 x16
        Current Usage: In Use
        Length: Long
        ID: 1
        Characteristics:
                3.3 V is provided
                PME signal is supported
        Bus Address: 0000:05:00.0


(or)
        Current Usage: Available

最少样本:

cat test.file | grep -i "ether\|gigabit\|10G" | cut -d ":" -f 1 | uniq | wc -l  ; # -> minus 1 -> "server ; output"

ServerA ; 0
    02:00.0 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Dell" "Device 04f8"
    02:00.1 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Dell" "Device 04f8"

ServerB ; 1 
    02:00.0 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Dell" "Device 04f8"
    02:00.1 "Ethernet controller" "Broadcom Inc. and subsidiaries" "NetXtreme BCM5720 2-port Gigabit Ethernet PCIe" "Dell" "Device 04f8"
    41:00.0 "Ethernet controller" "Intel Corporation" "Ethernet Controller 10-Gigabit X540-AT2" -r01 "Intel Corporation" "Ethernet 10G 2P X540-t Adapter"
    41:00.1 "Ethernet controller" "Intel Corporation" "Ethernet Controller 10-Gigabit X540-AT2" -r01 "Intel Corporation" "Ethernet 10G 2P X540-t Adapter"

ServerC ; 1
    18:00.0 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Dell" "Gigabit 4P I350-t rNDC"
    18:00.1 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Dell" "Gigabit 4P I350-t rNDC"
    18:00.2 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Dell" "Gigabit 4P I350-t rNDC"
    18:00.3 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Dell" "Gigabit 4P I350-t rNDC"
    65:00.0 "Ethernet controller" "Intel Corporation" "Ethernet Controller 10G X550T" -r01 "Intel Corporation" "Ethernet 10G 2P X550-t Adapter"
    65:00.1 "Ethernet controller" "Intel Corporation" "Ethernet Controller 10G X550T" -r01 "Intel Corporation" "Ethernet 10G 2P X550-t Adapter"

ServerD ; 0
    18:00.0 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Dell" "Gigabit 4P I350-t rNDC"
    18:00.1 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Dell" "Gigabit 4P I350-t rNDC"
    18:00.2 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Dell" "Gigabit 4P I350-t rNDC"
    18:00.3 "Ethernet controller" "Intel Corporation" "I350 Gigabit Network Connection" -r01 "Dell" "Gigabit 4P I350-t rNDC"

答案1

方法 1

从输出中可以看出lspci,每个条目都以总线号、设备号和功能号 (BDF) 开头。您可以将每个唯一的总线号视为一个单独的物理设备。

要对它们进行计数,您可以非常轻松地将条目剪切为公交车号码,删除重复项,然后使用 和 的组合对它们进行计数cutuniqwc

╭─user@machine ~/test ‹master●› 
╰─$ lspci -mm | grep Eth                               
01:00.0 Ethernet controller Broadcom Inc. and subsidiaries NetXtreme BCM5720 2-port Gigabit Ethernet PCIe Broadcom Inc. and subsidiaries Device 4160
01:00.1 Ethernet controller Broadcom Inc. and subsidiaries NetXtreme BCM5720 2-port Gigabit Ethernet PCIe Broadcom Inc. and subsidiaries Device 4160
19:00.0 Ethernet controller Broadcom Inc. and subsidiaries BCM57416 NetXtreme-E Dual-Media 10G RDMA Ethernet Controller -r01 Broadcom Inc. and subsidiaries Device 4160
19:00.1 Ethernet controller Broadcom Inc. and subsidiaries BCM57416 NetXtreme-E Dual-Media 10G RDMA Ethernet Controller -r01 Broadcom Inc. and subsidiaries Device 4160
3b:00.0 Ethernet controller Broadcom Inc. and subsidiaries BCM57416 NetXtreme-E Dual-Media 10G RDMA Ethernet Controller -r01 Broadcom Inc. and subsidiaries Device 4161
3b:00.1 Ethernet controller Broadcom Inc. and subsidiaries BCM57416 NetXtreme-E Dual-Media 10G RDMA Ethernet Controller -r01 Broadcom Inc. and subsidiaries Device 4161
╭─user@machine ~/test ‹master●› 
╰─$ lspci -mm | grep -i "ether\|gigabit\|10G" | cut -d ":" -f 1 | uniq | wc -l
3

方法 2

如果你使用systemd可预测的接口名称(也是基于 PCIe ID),您可以使用的输出ip来获得相同的结果:

╭─user@machine ~/test ‹master●› 
╰─$ ip link show
enp0s32f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
enp1s32f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
enp1s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
enp2s32f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
enp2s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000"
╭─user@machine ~/test ‹master●› 
╰─$ ip link show | grep enp | sed -E 's/.+?enp([0-9]).*/\1/' | uniq | wc -l
3

不包括内置网卡

现在,如果您想排除单个内置网卡(也是通过 PCI 连接,但直接通过主板 PCB 上的走线连接),只需从结果中减去 1。

bash支持算术运算,例如foo=3 && echo "$((foo - 1))"

相关内容