サポート/広告リンクモードの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

あなたの質問に予想される出力が表示されておらず、2つのブロック間の違いを出力したいかどうかわからないので、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

おすすめ記事