linuxbashgreplsusb

Return idVendor, idProduct and endpoints via lsusb


I want to return just some of the data of my lovely printer. (The name always contains STMicroelectronics printer

I can print out all plugged USB devices with the lsusb command. This will give me (first line obviously being the printer):

Bus 001 Device 004: ID 0483:5743 STMicroelectronics printer-80
Bus 001 Device 003: ID 0424:ec00 Microchip Technology, Inc. (formerly SMSC) SMSC9512/9514 Fast Ethernet Adapter
Bus 001 Device 002: ID 0424:9514 Microchip Technology, Inc. (formerly SMSC) SMC9514 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub`

I can now find the device's details via lsusb -vvv -d 0483:5743, which returns:

Bus 001 Device 004: ID 0483:5743 STMicroelectronics printer-80
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               2.00
  bDeviceClass            0 
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x0483 STMicroelectronics
  idProduct          0x5743 
  bcdDevice            1.00
  iManufacturer           1 Printer  
  iProduct                2 printer-80
  iSerial                 3 012345678AB
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength       0x0020
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0xc0
      Self Powered
    MaxPower                2mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           2
      bInterfaceClass         7 Printer
      bInterfaceSubClass      1 Printer
      bInterfaceProtocol      2 Bidirectional
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x01  EP 1 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
can't get device qualifier: Resource temporarily unavailable
can't get debug descriptor: Resource temporarily unavailable
Device Status:     0x0001
  Self Powered

Now... How can I just return these data via bash:

idVendor (0x0483)
idProduct (0x5743)
endpointOUT (0x01)
endpointIN (0x81)

There must be some grep/regex magic that I just couldn't manage to master.

Thanks for any help in advance!

Searing for solutions to extract the line containing the printer description with regex.


Solution

  • Using GNU sed

    $ sed -En 's/[ \t]*b?(id[vp][^ \t]*|endpoint)(address)?[ \t]+([^ \t]*).* (out|in)?.*/\l\1\4 (\3)/Ip' <(lsusb | awk '$0 ~ /STMicroelectronics printer-80/{print $6}' | xargs -I % sh -c "lsusb -vvv -d %")
    idVendor (0x0483)
    idProduct (0x5743)
    endpointOUT (0x01)
    endpointIN (0x81)