2017-02-13 122 views
3

你好,我正在使用嵌入式系統上安裝的debian系統。 PC有3個端口USB我們稱之爲A,B,C。我想根據USB端口執行不同的腳本。我怎樣才能做到這一點?Linux基於USB端口執行腳本

我發現了很多關於udev規則的文章,並且我有下面的規則,如果我連接一個usb,它會起作用。

ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z]1", SYMLINK+="usb_to_check", RUN+="/usr/local/bin/check-usb1.sh" 

我該如何擴展此規則才能工作只有當我連接設備讓我們來說說usb A?

+0

這Q是似乎沒有被有關編程爲StackOverflow上定義的USB端口號。它可能更適合於http://unix.stackexchange.com(Unix和Linux)或http://superuser.com。使用Q底部的'flag'鏈接並請主持人移動它。請不要在2個不同的網站上發佈相同的Q.請閱讀http://stackoverflow.com/help/how-to-ask http:// stackoverflow。com/help/dont-ask和http://stackoverflow.com/help/mcve在發佈更多Q​​之前。祝你好運。 – shellter

回答

0

lsusb輸出設備所連接的USB總線和USB端口。在輸出端,一些USB根集線器是內部USB集線器,以及藍牙和網絡攝像頭等,請參閱https://unix.stackexchange.com/questions/144029/command-to-determine-ports-of-a-device-like-dev-tty-usb0

您應該弄清楚您的外部端口與哪個USB總線連接。在我的電腦上所有外部USB端口都連接到Bus 01

要做這個檢查lsusb -t的輸出,然後附上一個usb設備並再次檢查輸出lsusb -t。那麼你知道什麼是 '不會忽略' 你的三個外部USB端口在你的設備內部USB結構樹:

內部USB端口:

/: Bus 04.Port 1: Dev 1, Class=root_hub, Driver=ohci_hcd/5p, 12M 
/: Bus 03.Port 1: Dev 1, Class=root_hub, Driver=ohci_hcd/5p, 12M 
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/5p, 480M 
    |__ Port 1: Dev 64, If 0, Class='bInterfaceClass 0x0e not yet handled', Driver=uvcvideo, 480M 
    |__ Port 1: Dev 64, If 1, Class='bInterfaceClass 0x0e not yet handled', Driver=uvcvideo, 480M 

外部USB端口:

/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/5p, 480M 
    |__ Port 3: Dev 116, If 0, Class='bInterfaceClass 0xe0 not yet handled', Driver=rndis_host, 480M 
    |__ Port 3: Dev 116, If 1, Class=data, Driver=rndis_host, 480M 

USB記憶棒連接到外部端口#2

/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/5p, 480M 
    |__ Port 2: Dev 119, If 0, Class=stor., Driver=usb-storage, 480M 
    |__ Port 3: Dev 116, If 0, Class='bInterfaceClass 0xe0 not yet handled', Driver=rndis_host, 480M 
    |__ Port 3: Dev 116, If 1, Class=data, Driver=rndis_host, 480M 

此程序後,您的'地址'您的外部USB端口

dmesg連接的USB設備總是與包含USB總線和端口號的行出現:

[186067.360139] usb 1-1: new high-speed USB device number 123 using ehci_hcd是總線001的端口001

[186067.360139] usb 1-2: new high-speed USB device number 123 using ehci_hcd是總線001的端口002

[186067.360139] usb 1-3: new high-speed USB device number 123 using ehci_hcd是總線001端口003

在您的腳本中,您使用命令dmesg | grep "usb 1" | tail -1(尾部-1裏grep的最後一個實例,見http://www.stackoverflow.com/questions/24014194/how-to-grep-the-last-occurence-of-the-line-pattern

你可以得到的端口號直接使用命令

dmesg | grep -o -P 'usb 1.{0,3}' | tail -1 | head -c 7 | tail -c 1(如果您的所有外部端口上Bus 001

Grep characters before and after match?http://www.unix.com/unix-for-dummies-questions-and-answers/28542-how-do-i-get-nth-character-string.html

因此,您可以獲得最新USB設備(您的設備)所連接的USB端口號,您可以在udev腳本中使用該端口號(if ...


你也可以找到在/dev/bus/usb/文件系統正即總線01端口1 USB總線樹結構/dev/bus/usb/001/001

看到http://www.linuxnix.com/find-usb-device-details-in-linuxunix-using-lsusb-command/