Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2015 09:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/c59b373ef320c9697710 to your computer and use it in GitHub Desktop.
Save anonymous/c59b373ef320c9697710 to your computer and use it in GitHub Desktop.
print ("List of PCI devices found in the system:")
print ("\nBUS DEV FUN VENID DEVID SDEVID SVENID Desc")
from os import path, access, R_OK # W_OK for write permission.
path_to_reg_exe_X = os.path.join('x:', os.sep, 'Windows', 'System32', 'reg.exe')
path_to_reg_exe_C = os.path.join('c:', os.sep, 'Windows', 'System32', 'reg.exe')
path_to_reg_exe_D = os.path.join('d:', os.sep, 'Windows', 'System32', 'reg.exe')
if path.exists(path_to_reg_exe_X) and path.isfile(path_to_reg_exe_X) and access(path_to_reg_exe_X, R_OK):
path_to_reg_exe = path_to_reg_exe_X
elif path.exists(path_to_reg_exe_C) and path.isfile(path_to_reg_exe_C) and access(path_to_reg_exe_C, R_OK):
path_to_reg_exe = path_to_reg_exe_C
elif path.exists(path_to_reg_exe_D) and path.isfile(path_to_reg_exe_D) and access(path_to_reg_exe_D, R_OK):
path_to_reg_exe = path_to_reg_exe_D
else:
print ("Either file is missing reg.exe or is not readable")
cmd = r'%s QUERY HKLM\system\currentcontrolset\enum\PCI' % path_to_reg_exe
(rc , stdout) = getstatusoutput(cmd)
tmp_main_level_reg_pci = stdout.split("\n")
for row1 in range(len(tmp_main_level_reg_pci)):
tmp_pci_info = ''
tmp_vendor_id = ''
tmp_device_id = ''
tmp_subdevice_id = ''
tmp_subvendor_id = ''
tmp_sub_level_reg_pci = ''
###########################################################
# Pull the VENDOR, DEVICE, SUB_VENDOR, and SUB_DEVICE IDs
tmp_main_level_pci_key = string.strip(tmp_main_level_reg_pci[row1])
pci_regex = re.compile(r'.+PCI\\VEN_(?P<VENDOR_ID>[0-9A-Z]{4})&DEV_(?P<DEVICE_ID>[0-9A-Z]{4})&SUBSYS_(?P<SUB_DEVICE_ID>[0-9A-Z]{4})(?P<SUB_VENDOR_ID>[0-9A-Z]{4})&REV.+',re.IGNORECASE)
if pci_regex.search(tmp_main_level_pci_key):
tmp_pci_info = pci_regex.search(tmp_main_level_pci_key)
tmp_vendor_id = tmp_pci_info.group('VENDOR_ID')
tmp_device_id = tmp_pci_info.group('DEVICE_ID')
tmp_subdevice_id = tmp_pci_info.group('SUB_DEVICE_ID')
tmp_subvendor_id = tmp_pci_info.group('SUB_VENDOR_ID')
###########################################################
# Pull the sub keys from each PCI device
tmp_command = r'%s query "%s"' % (path_to_reg_exe, tmp_main_level_pci_key)
(rc, stdout) = getstatusoutput(tmp_command)
tmp_sub_level_reg_pci = stdout.split("\n")
########################################################
# Pull the BUS, DEVICE and FUNCTION for each PCI device
for row2 in range(len(tmp_sub_level_reg_pci)):
tmp_sub_level_reg_pci_key = string.strip(tmp_sub_level_reg_pci[row2])
tmp_bus = ''
tmp_device = ''
tmp_function = ''
#print tmp_main_level_pci_key
tmp_command = r'%s query "%s" /v locationinformation' % (path_to_reg_exe, tmp_sub_level_reg_pci_key)
(rc, stdout) = getstatusoutput(tmp_command)
tmp_command = r'%s query "%s" /s /v DeviceDesc' % (path_to_reg_exe, tmp_sub_level_reg_pci_key)
(rc, stdout2) = getstatusoutput(tmp_command)
INFO = []
INFO = stdout2
INFO = INFO.split(";")
bdf_regex = re.compile(r'.+;\((?P<BUS>[0-9]{1,4}),(?P<DEVICE>[0-9]{1,2}),(?P<FUNCTION>[0-9]{1,2})\)',re.IGNORECASE)
tmp_bdf = string.strip(stdout)
if bdf_regex.search(tmp_bdf):
tmp_bdf_info = bdf_regex.search(tmp_bdf)
tmp_bus = tmp_bdf_info.group('BUS')
tmp_device = tmp_bdf_info.group('DEVICE')
tmp_function = tmp_bdf_info.group('FUNCTION')
if len(INFO) < 2:
DeviceDesc = "NO DESCRIPTION"
else:
DeviceDesc = str(INFO[1])
DeviceDescINFO = DeviceDesc.split("\n")
DeviceName = DeviceDescINFO[0]
if (tmp_bus != "0" and tmp_device == "0" and tmp_function == "0"):
number_of_devices = number_of_devices + 1
print ("%s %s %s %s %s %s %s %s" % (tmp_bus, tmp_device, tmp_function,tmp_vendor_id, tmp_device_id, tmp_subdevice_id ,tmp_subvendor_id, DeviceName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment