Skip to main content

py-lspci

Actions Status PyPI - Status PyPI - Python Version PyPI PyPI - License


py-lspci – parser for lspci output on remote or local UNIX machines. This package provides convenient interface to interact with lspci output in form of Python objects.

Getting Started

Prerequisites

py-lspci requires python 3.6 or newer versions to run. Also targets that you could interact with py-lspci must have pciutils installed on them.

Installing

Cloning project from git repository

git clone https://github.com/YADRO-KNS/py-lspci.git

Installing from PyPi

pip3 install py-lspci

Examples

Connection

First we have to establish connection to our target as user with sudo privileges:

import pylspci

scanner = pylspci.ScannerPCI(ip='192.168.1.1', username='admin', password='pa$$w0rd')

In cases if we targeting local machine we need to provide user password if user isn't root:

import pylspci

scanner = pylspci.ScannerPCI(ip='127.0.0.1', password='pa$$w0rd')

Select

With ScannerPCI object now we can write requests to get data from lspci output, main tool to do that is select method, that will return PCISelect iterator object.

>>> scanner.select()
<pylspci.pci_scanner.PCISelect object at 0x7fa1dcda3940>

Select will return all PCI devices that matches select request.

>>> scanner.select().count()
22
>>> scanner.select(pci_address='0000:00:00.0').count()
1

For broad select requests you could use asterisk:

>>> scanner.select(type='Bridge').count()
0
>>> scanner.select(type='*Bridge').count()
10

Use multiple keyword arguments to specify search. You could search by any attributes or properties of PCIDevice class.:

>>> scanner.select(type='*Bridge', is_upstream=True).count()
1

With PCISelect object you could loop over PCI devices that matches search parameters:

>>> for device in scanner.select(is_downstream=True):
...     print(device)
...
0000:08:00.0 PCI bridge Intel Corporation JHL6240 Thunderbolt 3 Bridge [x4/x4][2.5GT/s/2.5GT/s]
0000:08:01.0 PCI bridge Intel Corporation JHL6240 Thunderbolt 3 Bridge [x4/x4][2.5GT/s/2.5GT/s]
0000:08:02.0 PCI bridge Intel Corporation JHL6240 Thunderbolt 3 Bridge [x4/x4][2.5GT/s/2.5GT/s]

Also you can chain your select requests:

>>> scanner.select(type='PCI bridge').count()
8
>>> scanner.select(type='PCI bridge').select(is_upstream=True).count()
1

Get

Another search method is get. Basically it is the same select that will return first matching object instead of list of objects or will raise exception in case if there was no matches.

>>> print(scanner.get(type='*Host'))
0000:07:00.0 PCI bridge Intel Corporation [x2/x2][8GT/s/8GT/s]
>>> print(scanner.get(type='*Host', is_upstream=True))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/sergey/PycharmProjects/py-lspci/pylspci/pci_scanner.py", line 98, in get
    if parent.is_host_bridge:
pylspci.pci_scanner.DoesNotExist: Unable to find PCI Device matching: {'type': '*Host', 'is_upstream': True}

Get Connected

Another tool is get_connected method of Scanner, that returns PCISelect with all devices connected to passed device. For Host Bridge it will return all devices in Root Complex. For Upstream of PCI Bridge - all Downstreams. For Downstream or Root Ports - all connected Upstreams or Endpoints. End for Endpoints it will return empty list.

>>> scanner.get_connected(scanner.get(type='*Host')).count()
14

py-lspci uses cached value of lspci output, in case if you need to refresh that data, use force_rescan argument, for any of mentioned methods.

>>> scanner.select(force_rescan=True)

PCI rescan procedure

Last but not least method of ScannerPCI is pci_rescan that causes full rescan of PCI bus on target machine. Be careful with this one, because not all distros support proper PCI rescan.

Versioning

We use SemVer for versioning.

Authors

See also the list of contributors who participated in this project.

License

The code is available as open source under the terms of the MIT License.

Metadata

Release files for py-lspci 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-lspci 0.0.9
File Size Uploaded Provenance Actions
py-lspci-0.0.9.tar.gz 7.4 kB Without provenance Details for py-lspci-0.0.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py-lspci 0.0.9
File Interpreter ABI Platform Actions
py_lspci-0.0.9-py3-none-any.whl Python 3 none any Details for py_lspci-0.0.9-py3-none-any.whl

Total release size: 19.0 kB

Release files / py-lspci-0.0.9.tar.gz

Download URL py-lspci-0.0.9.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
e7c701e949796eaba95458ad47bc2038bfaed44b3714ca07c28b6be56fe1576e
BLAKE2b-256 checksum
How to use checksums
4ce28ac8c2abf14d305f7629341758f4e49869ef6f2b7f1628cf24a3770ce3fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/2.7.17

Release files / py_lspci-0.0.9-py3-none-any.whl

Download URL py_lspci-0.0.9-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c8b2eb529565efc376c4415394658f42b1fdd2bde2e4e89782ed29d6a4446db
BLAKE2b-256 checksum
How to use checksums
d9c59f96c376ef39acaa2a6b54c01e5a16d9e1d058621a97fef27b2b501ab3a5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.15.0 pkginfo/1.7.0 requests/2.25.1 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/2.7.17

Security for py-lspci 0.0.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page