列出 Linux 中的所有 USB 驱动器

发布于 2024-11-05 15:09:34 字数 80 浏览 6 评论 0原文

如何在 Linux 中获取可移动驱动器(插入 USB)的列表?如果使用 KDE、GNOME 或其他 DE 库能让事情变得更容易的话,我很乐意使用。

How can I get a list of removable drives (plugged into USB) in Linux? I'm fine with using KDE, GNOME or other DE libraries if it would make things easier.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

花伊自在美 2024-11-12 15:09:34

我认为一个好主意是使用 python 的 udev 接口

小例子(当然,在您的情况下,您已经调整了一些过滤):

In [1]: import pyudev
In [2]: pyudev.Context()
In [3]: ctx = pyudev.Context()
In [4]: list(ctx.list_devices(subsystem='usb'))
Out[4]: 
[Device(u'/sys/devices/pci0000:00/0000:00:1a.0/usb2'),
 Device(u'/sys/devices/pci0000:00/0000:00:1a.0/usb2/2-0:1.0'),
 Device(u'/sys/devices/pci0000:00/0000:00:1a.0/usb2/2-2'),

在大多数情况下,这是一个好方法,因为新系统使用 udev。

I think a nice idea is to use udev interface from python.

Small example (of course in your case you have adjust some filtering):

In [1]: import pyudev
In [2]: pyudev.Context()
In [3]: ctx = pyudev.Context()
In [4]: list(ctx.list_devices(subsystem='usb'))
Out[4]: 
[Device(u'/sys/devices/pci0000:00/0000:00:1a.0/usb2'),
 Device(u'/sys/devices/pci0000:00/0000:00:1a.0/usb2/2-0:1.0'),
 Device(u'/sys/devices/pci0000:00/0000:00:1a.0/usb2/2-2'),

It is a good way in most cases as new systems use udev.

饮湿 2024-11-12 15:09:34

过了这么久,问题又被解开了……

最后我通过 D-Bus 接口使用 UDisk,如此处所示。

After all this time the question got unlocked again…

In the end I used UDisks via the D‐Bus interface like shown here.

晨与橙与城 2024-11-12 15:09:34

不久前,我得到了这个小脚本(它不是我的),但它确实对我有很大帮助,仅供参考

#!/usr/bin/python
import sys
import usb.core
# find USB devices
dev = usb.core.find(find_all=True)
# loop through devices, printing vendor and product ids in decimal and hex
for cfg in dev:
      try:
              #print dir(cfg)
              sys.stdout.write('Decimal VendorID=' + str(cfg.idVendor) + ' & ProductID=' + str(cfg.bDeviceClass) + '  ' + str(cfg.product) + ' ' + str(cfg.bDeviceSubClass)+ '  ' + str(cfg.manufacturer)+'\n')
      except:
              print 

Sometime back i got this small script ( it's not mine ) but it surely helped me alot putting just for reference

#!/usr/bin/python
import sys
import usb.core
# find USB devices
dev = usb.core.find(find_all=True)
# loop through devices, printing vendor and product ids in decimal and hex
for cfg in dev:
      try:
              #print dir(cfg)
              sys.stdout.write('Decimal VendorID=' + str(cfg.idVendor) + ' & ProductID=' + str(cfg.bDeviceClass) + '  ' + str(cfg.product) + ' ' + str(cfg.bDeviceSubClass)+ '  ' + str(cfg.manufacturer)+'\n')
      except:
              print 
巷子口的你 2024-11-12 15:09:34

这是我在 bash 中使用的:
lsblk --pairs --nodeps | lsblk --pairs --nodeps | grep 'RM="1"'

示例输出:
NAME="sda" MAJ:MIN="8:0" RM="1" SIZE="59.5G" RO="0" TYPE="disk" MOUNTPOINT=""

请注意,它列出的是设备,而不是其分区。如果你也想看分区,
lsblk --对 | grep 'RM=“1”'

This is what I use from bash:
lsblk --pairs --nodeps | grep 'RM="1"'

Sample output:
NAME="sda" MAJ:MIN="8:0" RM="1" SIZE="59.5G" RO="0" TYPE="disk" MOUNTPOINT=""

Note it is listing the devices, not its partitions. If you like to see the partitions also,
lsblk --pairs | grep 'RM="1"'

晌融 2024-11-12 15:09:34

有什么理由不直接解析 lsusb 的结果?我确信有这方面的模块,但话又说回来,有时简单就是最好的。

我无法使用 Python 帮助你,在 Perl 中我可能会这样做:

#!/usr/bin/env perl

use strict;
use warnings;

my @data;
foreach (`lsusb`) {
  next unless /Bus (\S+) Device (\S+): ID (\S+) (.*)/;
  push @data, { bus => $1, device => $2, id => $3, info => $4 };
}

use Data::Printer;
p @data;

在我的计算机上,这会导致

[
    [0] {
        bus   005,
        device   001,
        id   "1d6b:0001",
        info   "Linux Foundation 1.1 root hub"
    },
    [1] {
        bus   004,
        device   001,
        id   "1d6b:0001",
        info   "Linux Foundation 1.1 root hub"
    },
    [2] {
        bus   003,
        device   001,
        id   "1d6b:0001",
        info   "Linux Foundation 1.1 root hub"
    },
    [3] {
        bus   002,
        device   001,
        id   "1d6b:0001",
        info   "Linux Foundation 1.1 root hub"
    },
    [4] {
        bus   001,
        device   003,
        id   "0bda:0158",
        info   "Realtek Semiconductor Corp. USB 2.0 multicard reader"
    },
    [5] {
        bus   001,
        device   002,
        id   "064e:a129",
        info   "Suyin Corp. "
    },
    [6] {
        bus   001,
        device   001,
        id   "1d6b:0002",
        info   "Linux Foundation 2.0 root hub"
    }
]

Note that Data::Printer and its p function are human-友好的对象倾倒仅用于检查目的。

Any reason not to just parse out the results from lsusb? I'm sure there are modules for this, but then again, easy is sometimes best.

I can't help you with Python, in Perl I might do:

#!/usr/bin/env perl

use strict;
use warnings;

my @data;
foreach (`lsusb`) {
  next unless /Bus (\S+) Device (\S+): ID (\S+) (.*)/;
  push @data, { bus => $1, device => $2, id => $3, info => $4 };
}

use Data::Printer;
p @data;

which, on my computer, results in

[
    [0] {
        bus   005,
        device   001,
        id   "1d6b:0001",
        info   "Linux Foundation 1.1 root hub"
    },
    [1] {
        bus   004,
        device   001,
        id   "1d6b:0001",
        info   "Linux Foundation 1.1 root hub"
    },
    [2] {
        bus   003,
        device   001,
        id   "1d6b:0001",
        info   "Linux Foundation 1.1 root hub"
    },
    [3] {
        bus   002,
        device   001,
        id   "1d6b:0001",
        info   "Linux Foundation 1.1 root hub"
    },
    [4] {
        bus   001,
        device   003,
        id   "0bda:0158",
        info   "Realtek Semiconductor Corp. USB 2.0 multicard reader"
    },
    [5] {
        bus   001,
        device   002,
        id   "064e:a129",
        info   "Suyin Corp. "
    },
    [6] {
        bus   001,
        device   001,
        id   "1d6b:0002",
        info   "Linux Foundation 2.0 root hub"
    }
]

Note that Data::Printer and its p function are human-friendly object dumping for inspection purposes only.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文