列出Linux中的所有USB驱动器
我该怎么在Linux系统中获取一个可移动硬盘(插入USB接口的)列表呢?如果使用KDE、GNOME或其他桌面环境的库能让事情更简单,我也没问题。
5 个回答
0
前段时间我找到了一段小脚本(这不是我写的),但它确实帮了我很多,所以我放在这里供大家参考。
#!/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
2
经过这么长时间,这个问题又被解锁了……
最后我通过D-Bus接口使用了UDisks,就像这里所示的那样。
5
我觉得一个不错的主意是用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。