2

私は ruby​​ の初心者で、これで頭がいっぱいになりました。「ethtool の出力をさまざまな変数に分割する必要があります。

これは私がやったことです:

[root@aptpka02 facter]# cat test.rb
interface = "enp8s0,enp9s0,enp1s0f0,enp1s0f1d1"
interface.split(',').each do |int|
 # call ethtool to get the driver for this NIC
  puts int
  ifline = %x{/sbin/ethtool -i #{int} 2>/dev/null }
  puts ifline

end

これは出力です(1つのインターフェースのみ):

enp1s0f1d1
driver: sfc
version: 4.0
firmware-version: 4.2.2.1003 rx1 tx1
bus-info: 0000:01:00.1
supports-statistics: yes
supports-test: yes
supports-eeprom-access: no
supports-register-dump: yes
supports-priv-flags: no

ドライバーとファームウェアの情報が必要なだけです。次のように、「または」を使用してgrepをコマンド実行に追加しようとしました。

interface.split(',').each do |int|
# call ethtool to get the driver for this NIC
puts int
ifline = %x{/sbin/ethtool -i #{int} 2>/dev/null | grep "driver\| firmware"}
puts ifline

end

動作しません。空の行が出力されます。

最後に、私がやりたいことは次のようなものです:

[root@aptpka02 facter]# vim test.rb
interface = "enp8s0,enp9s0,enp1s0f0,enp1s0f1d1"
interface.split(',').each do |int|
 # call ethtool to get the driver for this NIC
puts int
ifline = %x{/sbin/ethtool -i #{int} 2>/dev/null | grep "driver\| firmware"}.lines.each do | nicinfo|
 if (nicinfo = driver)
   driver = %x{/sbin/ethtool -i #{int} 2>/dev/null | grep 'driver: '}.chomp.sub("driver: ", "")
 else
  .
  .
  .
 endif

end

続ける方法のヒントを教えてください。

よろしくお願いします。

4

2 に答える 2

2
ifdata = ifline
  .lines                                       # array of \n-terminated lines
  .map { |line| line.chomp.split(': ', 2) }    # array of [key, value] pairs
  .select { |line| line.length > 1 }           # get rid of anomalous "enp1s0f1d1"
  .to_h                                        # hashify

ifdata['driver']
# => sfc
ifdata['firmware-version']
# => 4.2.2.1003 rx1 tx1
于 2018-07-19T05:43:23.710 に答える