2011-05-06 75 views
9

如何在Linux中獲得可移動驅動器列表(插入USB)?如果使用KDE,GNOME或其他DE庫,我會很輕鬆。列出Linux中的所有USB驅動器

+1

問題應該遷移到askubuntu – 2011-05-06 12:38:00

+11

@Viswanathan:「Linux」是不是Ubuntu的(這當然,爲什麼有一個單獨的askubuntu網站是愚蠢的,但我離題) – geoffspear 2011-05-06 12:41:57

+0

這是一個編程問題?因爲迄今爲止的答案與編程無關。 – Gabe 2011-05-06 13:04:49

回答

3

我認爲一個不錯的主意是從python使用udev interface

小例子(當然你的情況,你必須調整一些過濾):

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的好辦法。

0

任何理由不解釋從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" 
    } 
] 

注意Data::Printer及其p功能是人類只能用於檢查目的的傾倒物體。

+0

我對perl不太好,但是......我認爲一個更好的方法(通常)是使用udev或者一些usb模塊來進行perl而不是解析'ls'命令。 – spinus 2012-12-05 20:13:51

+0

當然,perl和python都有模塊用於這個目的,所以'lsusb'的輸出仍然有效並且能夠快速完成工作。與往常一樣,所採用的嚴格程度取決於作者和他/她的任務。 – 2012-12-05 23:22:41

2

這麼長的時間的問題再次得到了解鎖...

在結束後我通過d-bus接口使用UDisks像顯示here

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 
相關問題