You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何根据LTE USB Dongle的VID和PID获取对应网络接口名称?

获取LTE USB Dongle对应的网络接口名称

你已经通过VID和PID定位到了目标LTE设备,接下来要关联到它的网络接口名称,这里有两个实用的方案:

方案一:使用pyudev库(推荐)

我自己处理这类USB设备关联问题时,常用pyudev库——它能直接和系统的udev子系统交互,完美封装了设备间的关联逻辑,不用手动折腾文件路径。

首先安装pyudev:

pip install pyudev

然后修改你的代码,在找到目标设备后,通过pyudev匹配对应的网络接口:

import usb.core
import pyudev

def find_lte_interface():
    # 初始化pyudev上下文
    context = pyudev.Context()
    # 查找所有USB设备
    devices = usb.core.find(find_all=True)
    if devices is None:
        print("No Init. Check your Hardware Interfaces")
        exit(-1)
    
    target_vid = 0x1bbb
    target_pid = 0x0195
    
    for dev in devices:
        if dev.idVendor == target_vid and dev.idProduct == target_pid:
            print("===> Found LTE: Alcatel OneTouch L800")
            print("===> VID, PID = 0x%x, 0x%x" % (dev.idVendor, dev.idProduct))
            
            # 获取USB设备的bus和地址,用于精准匹配pyudev设备
            bus_num = dev.bus
            dev_addr = dev.address
            # 遍历匹配VID/PID的USB设备
            for udev_dev in context.list_devices(subsystem='usb', ID_VENDOR_ID=f'{target_vid:04x}', ID_MODEL_ID=f'{target_pid:04x}'):
                if udev_dev.attributes.asint('busnum') == bus_num and udev_dev.attributes.asint('devnum') == dev_addr:
                    # 找到该USB设备关联的网络设备
                    net_parent = udev_dev.find_parent(subsystem='net')
                    if net_parent:
                        interface_name = net_parent.properties.get('INTERFACE')
                        print(f"===> Corresponding network interface: {interface_name}")
                        return interface_name
    print("Target LTE device not found")
    return None

# 调用函数获取接口名称
find_lte_interface()

方案二:直接解析sysfs路径

如果不想额外安装依赖,可以通过Linux的sysfs文件系统直接查找——每个USB设备在sysfs里都有专属目录,其中会包含指向网络接口的符号链接。

修改你的代码如下:

import usb.core
import os

def find_lte_interface_sysfs():
    devices = usb.core.find(find_all=True)
    if devices is None:
        print("No Init. Check your Hardware Interfaces")
        exit(-1)
    
    target_vid = 0x1bbb
    target_pid = 0x0195
    
    for dev in devices:
        if dev.idVendor == target_vid and dev.idProduct == target_pid:
            print("===> Found LTE: Alcatel OneTouch L800")
            print("===> VID, PID = 0x%x, 0x%x" % (dev.idVendor, dev.idProduct))
            
            # 构建USB设备在sysfs中的路径
            sysfs_path = f"/sys/bus/usb/devices/{dev.bus}-{dev.address:03d}"
            # 检查该路径下的net目录(网络接口信息存放在这里)
            net_dir = os.path.join(sysfs_path, "net")
            if os.path.exists(net_dir):
                # net目录下的子目录名称就是网络接口名
                interface_names = os.listdir(net_dir)
                if interface_names:
                    print(f"===> Corresponding network interface(s): {', '.join(interface_names)}")
                    return interface_names[0]
    print("Target LTE device not found")
    return None

# 调用函数获取接口名称
find_lte_interface_sysfs()

这个方法依赖Linux的sysfs标准结构,主流发行版都能兼容,但如果是特殊定制系统可能需要微调路径。


内容的提问来源于stack exchange,提问作者Seunghyun Baek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 07:29:19