Created
June 5, 2019 03:35
-
-
Save Kuanlin-Chen/f52910c410706ef2bdc556a23b312ca8 to your computer and use it in GitHub Desktop.
Get USB bus/port information from serial number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
=begin | |
Pass the serial number of device to this program, it will print the corresponding usb bus/port info. | |
Example: | |
$ adb devices | |
List of devices attached | |
56789AAAAAAAAAA device | |
$ lsusb -v | |
Bus 003 Device 027: ID 05c7:1234 Corey, Inc. | |
Device Descriptor: | |
bLength 18 | |
...... | |
iProduct 2 Android | |
iSerial 3 56789AAAAAAAAAA | |
bNumConfigurations ...... | |
$ ruby get_port_from_serial.rb 56789AAAAAAAAAA | |
=> 003/027 | |
=end | |
def get_port_from_serial(serial, info) | |
info.each_line do |line| | |
if line.match('\bBus\b.*\bDevice\b') | |
@bus = line.split(' Device')[0].split(' ')[1] | |
@device = line.split(': ID')[0].split(' Device ')[1] | |
end | |
if line.match(serial) | |
return | |
end | |
end | |
@bus = "null" | |
@device = "null" | |
end | |
serial = ARGV[0] | |
info = `lsusb -v` | |
get_port_from_serial(serial, info) | |
puts "#{@bus}/#{@device}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment