adict11
adict11

Reputation: 15

linux ethernet drivers product id, device id and address

On a linux platform, I need to find the product_id, vendor_id and address of an interface given its name from cli. I am using the following commands to find it:

# 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)

Is there is a better approach to this? Since i have hard coded values like tail -c 8 above.

Can this be done in python? As the parent program is mostly a python module.

Appreciate any good inputs!

Upvotes: 0

Views: 2063

Answers (1)

larsks
larsks

Reputation: 312620

Is there is a better approach to this?

I would take advantage of the sysfs filesystem typically located in /sys. For example, given an interface name enp0s25 on my system, I can find the vendor in:

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

And the product id in:

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

The uevent file in the same directory contains this information in a form that can easily be sourced into a shell script:

$ 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

Upvotes: 1

Related Questions