Ruby Tutorial

Ruby program to get PCI addresses list on linux system (RHEL 7, Centos 7 / Fedora 26)

Ruby program to get list of available PCI addresses on linux system (RHEL 7, Centos 7 / Fedora 26)

This ruby program reads the available PCI addresses in system and prints the list of pci addresses in console.

PCI details can be captured by reading file '/proc/bus/pci/devices' in system and we can compute pci addresses using pci details.

if File.file?("/proc/bus/pci/devices")                                          
  drivers=File.read("/proc/bus/pci/devices")                                    
  drivers_lines=drivers.split("\n")                                             
else                                                                            
  drivers_lines=Array.new                                                       
end                                                                             
drivers=Hash.new                                                                
drivers_lines.each do |line|                                                    
  line = line.gsub(/^\s+|\s+$/m, '').split(" ")                                 
  if line.length == 18                                                          
    pci_details = line[0]                                                       
    driver = line[-1]                                                           
    bus = pci_details[0] + pci_details[1]                                       
    slot = ((pci_details[2].to_i(16) << 1) + (pci_details[3].to_i(16) >> 3)).to_s(16).upcase
    function = (pci_details[3].to_i(16) & 7).to_s(16).upcase                    
    pci_address = "0000:" + bus + ":" + slot + "." + function                   
    puts (pci_address)                                                          
  end                                                                           
end   

Output:

$ ruby pci.rb 
0000:00:2.0
0000:00:3.0
0000:00:14.0
0000:00:16.0
0000:00:16.3
0000:00:19.0
0000:00:1A.0
0000:00:1B.0
0000:00:1C.0
0000:00:1C.1
0000:00:1D.0
0000:00:1F.0
0000:00:1F.2
0000:00:1F.3
0000:02:0.0
0000:03:0.0

 Ruby Tutorial

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^