远程或本地计算机上lspci输出的解析器
py-lspci的Python项目详细描述
py lspci
py lspci–用于远程或本地UNIX计算机上lspci输出的解析器。 这个包提供了方便的接口,以Python对象的形式与lspci输出进行交互。在
入门
先决条件
pylspci需要python3.6或更新版本才能运行。 还必须安装与py lspci交互的目标pciutils 在他们身上。在
安装
从git存储库克隆项目
git clone https://github.com/YADRO-KNS/py-lspci.git
从PyPi安装
^{pr2}$示例
连接
首先,我们必须以具有sudo权限的用户身份建立与目标的连接:
importpylspciscanner=pylspci.ScannerPCI(ip='192.168.1.1',username='admin',password='pa$$w0rd')
在以本地计算机为目标的情况下,如果用户不是root用户,则需要提供用户密码:
importpylspciscanner=pylspci.ScannerPCI(ip='127.0.0.1',password='pa$$w0rd')
选择
使用ScannerPCI对象,我们现在可以编写从lspci输出获取数据的请求,主要的工具是 ^{str1}$select方法,它将返回PCISelect迭代器对象。在
>>> scanner.select()
<pylspci.pci_scanner.PCISelect object at 0x7fa1dcda3940>
Select将返回与Select请求匹配的所有PCI设备。在
>>> scanner.select().count()
22
>>> scanner.select(pci_address='0000:00:00.0').count()
1
对于广泛选择的请求,您可以使用星号:
>>> scanner.select(type='Bridge').count()
0
>>> scanner.select(type='*Bridge').count()
10
使用多个关键字参数指定搜索。 您可以按PCIDevice类的任何属性或属性进行搜索:
>>> scanner.select(type='*Bridge', is_upstream=True).count()
1
使用PCISelect对象,您可以在匹配搜索参数的PCI设备上循环:
>>> 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]
您还可以链接您的选择请求:
>>> scanner.select(type='PCI bridge').count()
8
>>> scanner.select(type='PCI bridge').select(is_upstream=True).count()
1
获取
另一个搜索方法是get。基本上它是相同的选择,将返回第一个匹配对象 如果没有匹配项,则将引发异常。在
>>> 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}
连接
另一个工具是Scanner的get_connected方法,它返回PCISelect,所有设备都连接到传递的设备。 对于主机网桥,它将返回根复合体中的所有设备。PCI桥上游-所有下游。 对于下游或根端口-所有连接的上游或端点。对于终结点,它将返回空列表。在
>>> scanner.get_connected(scanner.get(type='*Host')).count()
14
py lspci使用lspci输出的缓存值,如果需要刷新该数据,请使用force_rescan参数, 对于任何提到的方法。在
>>> scanner.select(force_rescan=True)
PCI重新扫描程序
ScannerPCI的最后一个但并非最不重要的方法是pci\u rescan,它会导致目标计算机上pci总线的完全重新扫描。 注意这个,因为不是所有发行版都支持正确的PCI重新扫描。在
版本控制
我们使用SemVer进行版本控制。在
作者
另请参阅参与本项目的contributors人员名单。在
许可证
根据MIT License的条款,代码可以作为开源代码使用。在
- 项目
标签: