3

I need to find and modify one line from the output of lsusb

pi@junior ~ $ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp. 
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 009: ID 12d1:1506 Huawei Technologies Co., Ltd. E398 LTE/UMTS/GSM Modem/Networkcard
Bus 001 Device 008: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp. 
Bus 001 Device 004: ID 05e3:0610 Genesys Logic, Inc. 4-port hub
Bus 001 Device 005: ID 05e3:0610 Genesys Logic, Inc. 4-port hub
Bus 001 Device 006: ID 04fc:05d8 Sunplus Technology Co., Ltd Wireless keyboard/mouse

So, grep to search:

pi@junior ~ $ lsusb | grep 12d1:1506
Bus 001 Device 009: ID 12d1:1506 Huawei Technologies Co., Ltd. E398 LTE/UMTS/GSM Modem/Networkcard

Now I need to modifiy the grep statement or pipe it into something else to get the following value:

/dev/bus/usb/001/009

Which I then need to pass into:

/usr/sbin/usbreset /dev/bus/usb/001/009

If I was coding, I would use a Regex:

Bus (\d{3}) Device (\d{3}).*15d1:1506.*

Replace with

/dev/bus/usb/\1/\2

Any idea how I can do this in a script?
I also need to know how to store that in a variable to be passed to usbreset as a parameter.

1
  • sed is your friend. You can use this page or search for "sed examples". Generally you just want a small shell script that does a few things, including passing it to usbreset. Hopefully someone will give you a more specific example, but these are the building blocks!
    – tedder42
    Aug 28, 2013 at 1:05

5 Answers 5

6

You don't have to use grep for this issue. lsusb can output with -d parameter to get information about given usb vendor:id number. (lsusb -d )

So your command should like this.

$(lsusb -d 12d1:1506 | awk -F '[ :]'  '{ print "/dev/bus/usb/"$2"/"$4 }' | xargs -I {} echo "./usbreset {}")

Save it with nano, chmod +x and you can invoke it with sudo.

2

In the end I used sed It turned out the usbreset wasn't the command for the job, I need to use a file called authorized which you echo 0 and then 1 to.

echo "Searching for $1"
devPath=`lsusb | grep $1 | sed -r 's/Bus ([0-9]{3}) Device ([0-9]{3}).*/bus\/usb\/\1\/\2/g;'`
echo "Found $1 @ $devPath"
echo "Searching for sysPath"
for sysPath in /sys/bus/usb/devices/*; do
    echo "$sysPath/uevent"
    devName=`cat "$sysPath/uevent" | grep $devPath`
    #echo devName=$devName
    if [ ! -z $devName ] 
    then
        break
    fi
done
if [ ! -z  $devName ] 
then
    echo "Found $1 @ $sysPath, Resetting"
    echo "echo 0 > $sysPath/authorized"
    echo 0 > $sysPath/authorized
    echo "echo 1 > $sysPath/authorized"
    echo 1 > $sysPath/authorized
else
    echo "Could not find $1"
fi

This allowed me to use familiar regex notation

1

I used awk.
Bus 001 Device 002: ID 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter

lsusb | awk -F '[ :]' ' /Ralink/ { print "/dev/bus/usb/"$2"/"$4 } '

/dev/bus/usb/001/002

If you wanted to use that with usbreset, then you could do this

# usbreset $(lsusb | awk -F '[ :]' ' /12d1:1506/ | { print "/dev/bus/usb/"$2"/"$4 }')

Theoretically...I haven't tested that.

1

Another solution which does not depend on awk, but only on simpler text processing utilities:

lsusb -d 12d1:1506 | tr ':' ' ' | cut -d ' ' -f 2,4 | xargs printf "/dev/bus/usb/%s/%s" | xargs usbreset
0

one command:

sudo /usr/sbin/usbreset $(ls /dev/bus/usb/001/$( lsusb | grep 12d1:1506 | cut -d ' ' -f 4 | cut -c-3))

I also understand you are using this: https://askubuntu.com/questions/645/how-do-you-reset-a-usb-device-from-the-command-line

1
  • 1
    You might want to explain what that command does, even in minor detail, just to make your answer more useful. Dec 6, 2014 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.