python - linux 以太网驱动程序产品 ID、设备 ID 和地址

标签 python linux bash awk ethernet

在 linux 平台上,我需要从 cli 中找到给定名称的接口(interface)的 product_id、vendor_id 和地址。我正在使用以下命令找到它:

# to find addr:
pci_interface_addr0=$(ethtool -i $eth0 | grep -i bus-info | tail -c 8)

# to find ids:
complete_id=$(lspci -nvv | grep $pci_interface_addr0 | awk '{print $3}')
vendor_id=$(echo $complete_id | cut -d \: -f 1)
product_id=$(echo $complete_id | cut -d \: -f 2)

有更好的方法吗?因为我有像上面的 tail -c 8 这样的硬编码值。

这可以在 python 中完成吗?由于父程序主要是一个 python 模块。

感谢任何好的输入!

最佳答案

Is there is a better approach to this?

我会利用通常位于 /sys 中的 sysfs 文件系统。例如,给定我系统上的接口(interface)名称 enp0s25,我可以在以下位置找到供应商:

$ cat /sys/class/net/enp0s25/device/vendor
0x8086

产品编号在:

$ cat /sys/class/net/enp0s25/device/device 
0x1502

同一目录中的 uevent 文件以一种可以很容易地获取到 shell 脚本的形式包含此信息:

$ cat /sys/class/net/enp0s25/device/uevent
DRIVER=e1000e
PCI_CLASS=20000
PCI_ID=8086:1502
PCI_SUBSYS_ID=17AA:21F3
PCI_SLOT_NAME=0000:00:19.0
MODALIAS=pci:v00008086d00001502sv000017AAsd000021F3bc02sc00i00

关于python - linux 以太网驱动程序产品 ID、设备 ID 和地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437537/

相关文章:

python - Pytorch 几何稀疏邻接矩阵到边索引张量

linux - 如何将 Oracle 中的数据库模式导出到转储文件

c++ - inotify - 在目录树中的几个级别之后无法将监视添加到新创建的目录

php - 如何在 Ubuntu 操作系统上使用 cron 作业每天运行 php 脚本

python - 将Python移植到D : urllib

python - numpy.直方图 : retrieve sum of weights squared in each bin

python - 根据阈值字典替换某些列中的值?

linux - 尾部 : cannot open ‘+2’ for reading: No such file or directory

linux - 在 bash 中,只有一个变量的 if 语句的计算结果是什么?

python - 想要在同一个 python 脚本中比较 bash 和 python 的时间