pylspci - PCI Device Parser

0.4.3 · active · verified Thu Apr 16

pylspci is a Python library that provides a convenient interface to parse the output of the `lspci -mmnn` command, transforming it into Python objects. It can be used to query PCI devices on local or remote UNIX machines. The current version is 0.4.3, with an infrequent release cadence, though a 0.5.0 release candidate was published in January 2025.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a target (local or remote) using `pylspci.ScannerPCI` and then retrieve and filter PCI devices. It uses environment variables for sensitive credentials in remote connection examples. Ensure the target machine has `pciutils` installed and appropriate network access and authentication are configured.

import os
from pylspci import ScannerPCI

# Example for a remote connection, using environment variables for credentials
# For local scan, if current user can run lspci without sudo, password can be omitted.
# If local scan requires sudo, provide password or ensure user has sudo access without password.

# Replace with your target IP, username, and ensure PCI_PASSWORD is set in your environment
target_ip = os.environ.get('PCI_HOST_IP', '127.0.0.1')
username = os.environ.get('PCI_USERNAME', 'root')
password = os.environ.get('PCI_PASSWORD', '')

try:
    # Connect to the target machine
    # For local machine requiring sudo, you'd typically pass ip='127.0.0.1', password=password
    # For remote, provide IP, username, and password.
    if target_ip == '127.0.0.1' and not password: # Attempt local without password if not explicitly set
        scanner = ScannerPCI(ip=target_ip)
    elif target_ip == '127.0.0.1': # Local with password
        scanner = ScannerPCI(ip=target_ip, password=password)
    else: # Remote connection
        scanner = ScannerPCI(ip=target_ip, username=username, password=password)

    # Select all PCI devices
    devices = scanner.select()

    print(f"Found {len(devices)} PCI devices:")
    for device in devices:
        print(f"  - {device.slot}: {device.vendor_name} {device.device_name} ({device.device_class_name})")

    # Example: Find all network controllers
    network_controllers = scanner.select(device_class_name='*Ethernet*')
    if network_controllers:
        print("\nNetwork Controllers:")
        for nc in network_controllers:
            print(f"  - {nc.slot}: {nc.vendor_name} {nc.device_name}")
    else:
        print("\nNo network controllers found.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →