比较支持/通告链接模式的 ethtool 输出

比较支持/通告链接模式的 ethtool 输出

我正在尝试捕获 ethtool 的“支持的链接模式:”和“广告链接模式:”的输出,以查看我的卡是否具有更高的支持,但交换机端不支持。所以,我试图使用 awk 或 sed 来捕获模式列表的输出,但我找不到一种方法来捕获该部分以便能够进行比较。任何想法?

ethtool em1
Settings for em1:
        Supported ports: [ FIBRE ]
        Supported link modes:   1000baseKX/Full
                                10000baseKR/Full
                                25000baseCR/Full
                                25000baseKR/Full
                                25000baseSR/Full
        Supported pause frame use: Symmetric
        Supports auto-negotiation: Yes
        Supported FEC modes: None BaseR
        Advertised link modes:     1000baseKX/Full
                                10000baseKR/Full
                                25000baseCR/Full
                                25000baseKR/Full
                                25000baseSR/Full
        Advertised pause frame use: Symmetric
        Advertised auto-negotiation: Yes
        Advertised FEC modes: None
        Speed: 10000Mb/s
        Duplex: Full
        Port: FIBRE
        PHYAD: 0
        Transceiver: internal
        Auto-negotiation: on
        Supports Wake-on: g
        Wake-on: d
        Current message level: 0x00000004 (4)
                               link
        Link detected: yes

这是预期的输出:

Supported link modes:      1000baseKX/Full
                                10000baseKR/Full
                                25000baseCR/Full
                                25000baseKR/Full
                                25000baseSR/Full
Advertised link modes:    1000baseKX/Full
                                10000baseKR/Full
                                25000baseCR/Full
                                25000baseKR/Full
                                25000baseSR/Full

我计划对此输出执行的操作是,如果广告链接少于支持的链接,则将广告链接与支持的链接进行比较

这是我想出的解决方案。我确信这可以改进:

#!/bin/bash

# Get the interface name
iface=$1

# Get the supported link modes
supported=$(ethtool $iface  | awk '/Supported link modes:/{mode=$NF; getline; while(/[[:space:]]+[0-9]+/){mode=mode" "$NF;getline}} END{print mode}')

# Get the advertised link modes
advertised=$(ethtool $iface  | awk '/Advertised link modes:/{mode=$NF; getline; while(/[[:space:]]+[0-9]+/){mode=mode" "$NF;getline}} END{print mode}')

# Compare the supported and advertised link modes
if [ "$supported" == "$advertised" ]; then
  echo "The supported and advertised link modes match."
else
  echo "The supported and advertised link modes do not match."
fi

答案1

您没有在问题中显示预期的输出,也不清楚您是否想输出两个块之间的差异,所以我认为您可能会要求使用任何 POSIX awk:

$ cat tst.awk
{
    gsub(/^[[:space:]]+|[[:space:]]+$/,"")
    if ( s = index($0,":") ) {
        tag = substr($0,1,s-1)
        val = substr($0,s+1)
        sub(/^[[:space:]]+/,"",val)
    }
    tag2val[tag] = (tag in tag2val ? tag2val[tag] ORS : "") val
}

END {
    print "Supported link modes:"
    print tag2val["Supported link modes"]

    print ""

    print "Advertised link modes:"
    print tag2val["Advertised link modes"]
}

$ awk -f tst.awk file
Supported link modes:
1000baseKX/Full
1000baseKX/Full
1000baseKX/Full
1000baseKX/Full
1000baseKX/Full
1000baseKX/Full

Advertised link modes:
1000baseKX/Full
1000baseKX/Full
1000baseKX/Full
1000baseKX/Full
1000baseKX/Full
1000baseKX/Full

相关内容