Skip to main content

Parser for lspci output on remote or local machines

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py-lspci-0.0.9.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

py_lspci-0.0.9-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file py-lspci-0.0.9.tar.gz.

File metadata

  • Download URL: py-lspci-0.0.9.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for py-lspci-0.0.9.tar.gz
Algorithm Hash digest
SHA256 e7c701e949796eaba95458ad47bc2038bfaed44b3714ca07c28b6be56fe1576e
MD5 f8c2eb012d0e3137e0d6ca9b823d6308
BLAKE2b-256 4ce28ac8c2abf14d305f7629341758f4e49869ef6f2b7f1628cf24a3770ce3fc

See more details on using hashes here.

File details

Details for the file py_lspci-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: py_lspci-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for py_lspci-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 2c8b2eb529565efc376c4415394658f42b1fdd2bde2e4e89782ed29d6a4446db
MD5 8bf5ded73becf8275e825f6d8a687a48
BLAKE2b-256 d9c59f96c376ef39acaa2a6b54c01e5a16d9e1d058621a97fef27b2b501ab3a5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page