greping 或过滤 linux 中的 ruby 输出以获取自定义事实
greping or filtering linux output in ruby for custom fact
我是 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
这是输出(仅针对一个接口):
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
我只需要驱动程序和固件信息,我尝试将带有 "or" 的 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
你能给我一个如何继续的提示吗?
在此先感谢您的帮助!
String#scan 在这种情况下很方便。假设您的示例数据位于名为 data:
的字符串中
data.scan(/(firmware-version: |driver: )(.+)/)
这输出一个数组:
=> [["driver: ", "sfc"], ["firmware-version: ", "4.2.2.1003 rx1 tx1"]]
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
我是 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
这是输出(仅针对一个接口):
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
我只需要驱动程序和固件信息,我尝试将带有 "or" 的 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
你能给我一个如何继续的提示吗?
在此先感谢您的帮助!
String#scan 在这种情况下很方便。假设您的示例数据位于名为 data:
的字符串中data.scan(/(firmware-version: |driver: )(.+)/)
这输出一个数组:
=> [["driver: ", "sfc"], ["firmware-version: ", "4.2.2.1003 rx1 tx1"]]
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