Created
October 25, 2013 04:53
-
-
Save lsongdev/7149576 to your computer and use it in GitHub Desktop.
lsusb for mac os
This file contains hidden or 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 bash | |
# lsusb (list USB devices) utility for Mac OS X | |
# Author: JLH, Sep 2013 | |
# Usage: | |
# | |
# lsusb [ options ] | |
# | |
# Options: | |
# -v | |
# Print the original output of `system_profiler SPUSBDataType` | |
# | |
# -d [vendor][:product] | |
# Show only devices with the specified vendor and product ID. Both ID's are given in hexadecimal. | |
# | |
# Disclaimer: some part of the usage info has been taken from the manual of lsusb(8), I take no credit for it. | |
# Save the data from the system_profiler command | |
system_profiler SPUSBDataType > sp_info.log | |
# Parse options (v and d for now) | |
while getopts "vd:" opt; do | |
case "$opt" in | |
# Verbose | |
v) | |
# Just output | |
cat sp_info.log | |
rm sp_info.log | |
exit 0 | |
;; | |
# Filter by VID/PID | |
d) | |
vid_pid=$OPTARG | |
;; | |
esac | |
done | |
# Get all sets of lines that contain the "Product ID" string. | |
# Also include other relevant lines. Here's an entry sample: | |
# | |
# BRCM2046 Hub: | |
# | |
# Product ID: 0x4500 | |
# Vendor ID: 0x0a5c (Broadcom Corp.) | |
# Version: 1.00 | |
# Speed: Up to 12 Mb/sec | |
# Manufacturer: Apple Inc. | |
# Location ID: 0x06100000 / 2 | |
# Current Available (mA): 500 | |
# Current Required (mA): 0 | |
# | |
# Each entry will be separated by two '--' lines. We replace them | |
# with the '#' symbol in case other parameters that contain a dash | |
# will not be interfered | |
devices=`grep -B 2 -A 6 "Product ID" sp_info.log | sed 's/^--/#/'` | |
# Change the IFS to #, backup the current one | |
OIFS=$IFS | |
IFS="#" | |
counter=0 | |
# Iterate over each entry | |
for device in $devices | |
do | |
# Get the name of the device, it is the first line that ends with a ':' | |
# Trim the string at the end | |
name=`echo $device | head -2 | grep ":$" | sed -e 's/^ *//g' -e 's/ *:$//g'` | |
# Get the PID, trim at the end | |
PID=`echo $device | grep "Product ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g'` | |
# If there's no PID then we skip the entry | |
if [ -z "$PID" ]; then | |
continue | |
fi | |
# Get the VID, trim at the end | |
VID_all=`echo $device | grep "Vendor ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g'` | |
# If there's no VID then we skip the entry | |
if [ -z "$VID_all" ]; then | |
continue | |
fi | |
VID=`echo $VID_all | awk -F ' ' '{print $1}' | sed 's/^ *//; s/ *$//'` | |
# Get the manufacturer string | |
manufacturer=`echo $VID_all | cut -d ' ' -f 2- | sed 's/^ *//; s/ *$//'` | |
# Filter by VID/PID if given as input argument | |
if [ -n "$vid_pid" ]; then | |
# Convert input vid to lower case | |
arg_vid=`echo $vid_pid | awk -F':' '{print $1}' | sed 's/^0x//' | tr '[A-Z]' '[a-z]'` | |
if [ -n "$arg_vid" ]; then | |
if [ $arg_vid != $VID ]; then | |
continue | |
fi | |
fi | |
# Convert input pid to lower case | |
arg_pid=`echo $vid_pid | awk -F':' '{print $2}' | sed 's/^ *//; s/ *$//; s/^0x//' | tr '[A-Z]' '[a-z]'` | |
if [ -n "$arg_pid" ]; then | |
# Remove manufacturer from PID | |
#pid_stripped=`echo $PID | awk -F ' ' '{print $1}' | sed 's/ *$//'` | |
if [ $arg_pid != $PID ]; then | |
continue | |
fi | |
fi | |
fi | |
# Get the bus number. For that, grep the last entry that contains "Bus Number" and then trim at the end. | |
# Also remove the 0x from hex numbers | |
bus_num=`grep -B 100 "$PID" sp_info.log | grep "Bus Number" | tail -1 | sed -e 's/Bus Number://' -e 's/0x//; s/^ *//g' -e 's/ *$//g'` | |
# Get the device number within the bus, print it as the second entry using '/' as field separator | |
device_num=`echo $device | grep "Location ID" | sed -e 's/Location ID://; s/^ *//g; s/ *$//g;' | awk -F'/' '{print $2}'` | |
# Include serial number only if available | |
serial_str="" | |
serial_number=`echo $device | grep "Serial Number" | sed 's/Serial Number: //; s/^ *//g; s/ *$//g'` | |
if [ -n "$serial_number" ]; then | |
serial_str=" Serial: $serial_number" | |
fi | |
# Print the formatted entry | |
echo "Bus $bus_num Device$device_num: ID $VID:$PID $manufacturer $name $serial_str" | |
done | |
# Restore the IFS | |
IFS=$OIFS | |
# Clean the log | |
rm sp_info.log |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment