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.
如何在Linux中获取可移动驱动器(插入USB)的列表?如果使用KDE、GNOME或其他DE库会使事情更容易一些,我可以接受。
4 个解决方案
#1
4
I think a nice idea is to use udev interface from python.
我认为从python中使用udev接口是一个不错的主意。
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.
在大多数情况下,当新系统使用udev时,这是一种很好的方式。
#2
2
After all this time the question got unlocked again…
经过这么长时间,这个问题又被解开了……
In the end I used UDisks via the D‐Bus interface like shown here.
最后,我通过D‐Bus接口使用了udisk,如图所示。
#3
0
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
#4
-1
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.
有什么理由不去分析lsusb的结果吗?我确信这其中有一些模块,但话说回来,简单有时是最好的。
I can't help you with Python, in Perl I might do:
我无法帮助您使用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;
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.
注意,数据::打印机及其p函数仅为人类友好的对象转储,仅供检查使用。
#1
4
I think a nice idea is to use udev interface from python.
我认为从python中使用udev接口是一个不错的主意。
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.
在大多数情况下,当新系统使用udev时,这是一种很好的方式。
#2
2
After all this time the question got unlocked again…
经过这么长时间,这个问题又被解开了……
In the end I used UDisks via the D‐Bus interface like shown here.
最后,我通过D‐Bus接口使用了udisk,如图所示。
#3
0
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
#4
-1
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.
有什么理由不去分析lsusb的结果吗?我确信这其中有一些模块,但话说回来,简单有时是最好的。
I can't help you with Python, in Perl I might do:
我无法帮助您使用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;
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.
注意,数据::打印机及其p函数仅为人类友好的对象转储,仅供检查使用。