我想查看已连接的 USB 设备(例如 USB 鼠标、USB 键盘)的列表,而无需浏览包含内部和外部存储设备(例如内部 HDD、USB 记忆棒等)的设备列表。我看过 lsusb 和 lsblk 但正在努力寻找解决方案。
是否有 grep 过滤器或替代命令可以实现此目的?
答案1
您可以grep -v "Mass Storage"
使用,lsusb
但如果在某些边缘情况下该字符串可能不会出现,我不会感到惊讶。
答案2
sudo lshw -businfo | grep -i usb | grep -v storage
我个人会过滤掉主机控制器和集线器,因为您不太可能想看到它们,所以它将是:
sudo lshw -businfo | grep -i usb | grep -v -E "storage| Host Controller| Hub"
答案3
想法:查询列出的每个设备lsusb
并仅打印属于未注册为“大容量存储”设备的设备的行;
相当多的研究告诉我(希望是正确的,如果这不像我理解的那么可靠,请指出),可以通过查询 USB 设备是否注册为“大容量存储”设备来确定/sys/bus/usb
正确总线/设备对的文件系统层次结构并提取(如果设置)其bInterfaceClass
值;如果是08
,则该设备是“大容量存储”设备。
当然,这更适合作为自己的脚本,但为了快速运行它:
lsusb | perl -wne '
use strict;
my ($pbus, $pdev) = map {int($_)} /^Bus ([0-9]+) Device ([0-9]+)/;
my $f = "/sys/bus/usb/devices/usb${pbus}/${pbus}-${pdev}/${pbus}-${pdev}:1.0/bInterfaceClass";
if (-f $f) {
open(my $fh, "<", $f);
my $bic = <$fh>;
chomp($bic);
next if $bic eq "08";
}
print
'
比较lsusb
上面脚本的输出和输出(注意缺少 Seagate 条目):
user@[REDACTED] ~ % lsusb
Bus 002 Device 003: ID 0bc2:aa15 Seagate RSS LLC Basic
Bus 002 Device 002: ID 0424:5537 Microchip Technology, Inc. (formerly SMSC) USB5537B
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 001 Device 004: ID 04d8:0b29 Microchip Technology, Inc. U2417H_0B29_15083001
Bus 001 Device 002: ID 0424:2137 Microchip Technology, Inc. (formerly SMSC) USB2137B
Bus 001 Device 005: ID 8087:0033 Intel Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
user@[REDACTED] ~ % lsusb | perl -wne '
use strict;
my ($pbus, $pdev) = map {int($_)} /^Bus ([0-9]+) Device ([0-9]+)/;
my $f = "/sys/bus/usb/devices/usb${pbus}/${pbus}-${pdev}/${pbus}-${pdev}:1.0/bInterfaceClass";
if (-f $f) {
open(my $fh, "<", $f);
my $bic = <$fh>;
chomp($bic);
next if $bic eq "08";
}
print
'
Bus 002 Device 002: ID 0424:5537 Microchip Technology, Inc. (formerly SMSC) USB5537B
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 001 Device 004: ID 04d8:0b29 Microchip Technology, Inc. U2417H_0B29_15083001
Bus 001 Device 002: ID 0424:2137 Microchip Technology, Inc. (formerly SMSC) USB2137B
Bus 001 Device 005: ID 8087:0033 Intel Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub