Linux系统用户的专属福利:除了lsusb,如何利用usb.ids文件离线查询所有USB设备VID/PID信息?
Linux系统深度实践离线高效查询USB设备VID/PID的完整指南当你身处没有网络连接的机房或是调试嵌入式设备时突然需要确认一个USB设备的厂商信息该怎么办对于Linux系统用户来说答案就藏在系统深处的一个小文件中——usb.ids。这个不起眼的文本文件实际上是Linux系统管理USB设备的秘密武器。1. 理解USB设备标识体系每个USB设备都带有两个关键标识符VIDVendor ID和PIDProduct ID。这两个16位数字组合构成了USB设备的身份证VID由USB-IFUSB Implementers Forum分配给设备制造商PID由制造商自行定义标识具体产品型号在Linux系统中这些标识符的映射关系被存储在一个结构化的文本数据库中。不同于需要联网查询的在线服务本地usb.ids文件提供了随时可用的离线查询能力特别适合以下场景无网络环境的服务器机房嵌入式设备开发调试自动化脚本中的设备识别批量处理多个USB设备信息2. 定位系统中的usb.ids文件不同Linux发行版中usb.ids文件可能存放在以下几个位置文件路径常见发行版备注/usr/share/misc/usb.idsDebian/Ubuntu, Fedora最普遍的位置/var/lib/usbutils/usb.ids较新的系统由usbutils包维护/usr/share/hwdata/usb.ids某些Red Hat系系统替代位置要确认你的系统中是否存在该文件可以执行find /usr /var -name usb.ids 2/dev/null如果系统中没有找到该文件可能需要安装usbutils软件包# Debian/Ubuntu sudo apt install usbutils # RHEL/CentOS sudo yum install usbutils3. 解析usb.ids文件结构usb.ids文件采用层级化的文本格式主要包含三部分内容厂商列表以VID开头后接厂商名称产品列表在厂商条目下以PID开头后接产品名称设备类信息定义USB设备类、子类和协议代码典型条目示例1234 Example Manufacturer 5678 Sample Product A 9abc Sample Product B理解这个结构后我们可以利用各种文本处理工具从中提取所需信息。4. 实战查询技巧4.1 基础查询方法使用grep命令进行简单查询# 查询特定VID对应的厂商 grep -i ^1234 /usr/share/misc/usb.ids # 查询特定VID和PID组合 grep -A1 -i ^1234 /usr/share/misc/usb.ids | grep -i 56784.2 高级查询脚本对于更复杂的查询需求可以创建可重用的脚本#!/bin/bash USBIDS_FILE/usr/share/misc/usb.ids if [ $# -lt 1 ]; then echo Usage: $0 VID [PID] exit 1 fi VID$(echo $1 | tr [:lower:] [:upper:]) PID$2 if [ -z $PID ]; then # 只查询VID grep -m1 -i ^${VID} $USBIDS_FILE else # 查询VID和PID组合 PID$(echo $PID | tr [:lower:] [:upper:]) awk -v vid$VID -v pid$PID BEGIN {found_vid0} $1 vid {found_vid1; print; next} found_vid /^\t/ $1 pid {print; exit} found_vid /^\t/ $1 ! pid {next} found_vid !/^\t/ {exit} $USBIDS_FILE fi将此脚本保存为usbquery并赋予执行权限后可以方便地查询./usbquery 093A ./usbquery 093A 25104.3 批量处理多个设备当需要处理多个USB设备信息时可以结合lsusb和脚本处理lsusb | awk {print $6} | while IFS: read vid pid; do echo Device $vid:$pid: ./usbquery $vid $pid done5. 维护与更新本地数据库虽然离线查询很方便但usb.ids文件需要定期更新以包含最新的设备信息。5.1 手动更新方法# 下载最新usb.ids文件 sudo wget http://www.linux-usb.org/usb.ids -O /usr/share/misc/usb.ids # 或者使用curl sudo curl -o /usr/share/misc/usb.ids http://www.linux-usb.org/usb.ids5.2 自动化更新创建定期更新的cron任务# 每月1号凌晨3点更新 0 3 1 * * root /usr/bin/curl -s -o /usr/share/misc/usb.ids http://www.linux-usb.org/usb.ids5.3 验证文件完整性更新后建议检查文件有效性head -n 10 /usr/share/misc/usb.ids # 应该看到类似内容 # # List of USB IDs # # # # Maintained by Stephen J. Gowdy linux.usb.idsgmail.com # # If you have any new entries, please submit them via # # http://www.linux-usb.org/usb-ids.html6. 系统集成与应用实例6.1 在Python中解析usb.idsdef get_usb_info(vid, pidNone): with open(/usr/share/misc/usb.ids, r, encodingiso-8859-1) as f: current_vid None for line in f: line line.strip() if not line or line.startswith(#): continue if not line.startswith(\t): # VID行 parts line.split(maxsplit1) if len(parts) 2 and parts[0].lower() vid.lower(): current_vid parts[1] if pid is None: return current_vid elif current_vid and pid: # PID行 parts line.strip().split(maxsplit1) if len(parts) 2 and parts[0].lower() pid.lower(): return f{current_vid} - {parts[1]} return None6.2 与udev规则结合创建自定义udev规则自动识别特定设备# /etc/udev/rules.d/99-usb-custom.rules ACTIONadd, SUBSYSTEMusb, ENV{ID_VENDOR_ID}1234, ENV{ID_MODEL_ID}5678, SYMLINKmy_device6.3 嵌入式系统中的应用考虑在资源受限的嵌入式系统中可以只保留需要的VID/PID条目减小文件大小将文件放入只读文件系统节省空间使用压缩版本如usb.ids.gz节省存储# 创建精简版usb.ids grep -e ^1234 -e ^abcd /usr/share/misc/usb.ids minimal.usb.ids7. 性能优化与高级技巧对于频繁查询的应用场景可以考虑以下优化方案7.1 数据库转换将文本文件转换为SQLite数据库提高查询效率#!/bin/bash INPUT/usr/share/misc/usb.ids OUTPUT/var/lib/usb_ids.db # 创建SQLite数据库 sqlite3 $OUTPUT EOF CREATE TABLE vendors (vid TEXT PRIMARY KEY, name TEXT); CREATE TABLE products (vid TEXT, pid TEXT, name TEXT, PRIMARY KEY (vid, pid)); BEGIN; EOF # 解析并导入数据 awk -v DB$OUTPUT /^#/ {next} /^[^\t]/ NF2 { vid $1 name substr($0, index($0,$2)) print INSERT INTO vendors VALUES (\ vid \, \ name \); DB } /^\t/ NF2 { pid $1 name substr($0, index($0,$2)) print INSERT INTO products VALUES (\ vid \, \ pid \, \ name \); DB } $INPUT # 提交事务并创建索引 sqlite3 $OUTPUT EOF COMMIT; CREATE INDEX idx_products_vid ON products (vid); CREATE INDEX idx_products_pid ON products (pid); EOF7.2 内存缓存方案对于需要极低延迟的应用可以将数据加载到内存中import sqlite3 from collections import defaultdict class USBCache: def __init__(self, db_path/var/lib/usb_ids.db): self.conn sqlite3.connect(db_path) self.vendor_cache {} self.product_cache defaultdict(dict) self._load_data() def _load_data(self): # 加载厂商数据 for vid, name in self.conn.execute(SELECT vid, name FROM vendors): self.vendor_cache[vid.lower()] name # 加载产品数据 for vid, pid, name in self.conn.execute(SELECT vid, pid, name FROM products): self.product_cache[vid.lower()][pid.lower()] name def get_vendor(self, vid): return self.vendor_cache.get(vid.lower()) def get_product(self, vid, pid): return self.product_cache.get(vid.lower(), {}).get(pid.lower())7.3 与系统监控工具集成结合lsusb和usb.ids创建增强型监控脚本#!/bin/bash # 增强版lsusb显示完整厂商和设备信息 lsusb | while read -r line; do bus$(echo $line | awk {print $2}) dev$(echo $line | awk {print $4} | tr -d :) vid_pid$(echo $line | awk {print $6}) vid${vid_pid%:*} pid${vid_pid#*:} vendor$(./usbquery $vid) product$(./usbquery $vid $pid) printf Bus %s Device %s: ID %s %s\n $bus $dev $vid_pid $product done
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2569347.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!