Mac OSX Gets USB Provider ID and Product ID

I am new to the Mac OSX world and I need to write a script that gives me the seller ID and product ID of the connected USB device. I did this for Windows and Linux, but for Mac I have no idea where to start.

I saw this post , but the link to this example does not work. Do you have any tips on where I can start or where you can find some examples?

In particular, which language should I use?

+6
source share
3 answers

You marked your question with bash , so I will answer it as if you were asking how to do it in bash, instead of asking which language to use (which would make the question off-topic for StackOverflow).

You can analyze existing data from system_profiler using the built-in tools. For example, here is a supplier dump: product pairs, with a "Location ID" and a manufacturer ...

 #!/bin/bash shopt -s extglob while IFS=: read key value; do key="${key##+( )}" value="${value##+( )}" case "$key" in "Product ID") p="${value% *}" ;; "Vendor ID") v="${value%% *}" ;; "Manufacturer") m="${value}" ;; "Location ID") l="${value}" printf "%s:%s %s (%s)\n" "$v" "$p" "$l" "$m" ;; esac done < <( system_profiler SPUSBDataType ) 

It depends on the fact that the Location ID is the last item specified for each USB device that I have not verified conclusively. (It seems to me that this is so.)

If you need something more readable (1) and (2) independent of bash and therefore more portable (not a problem, but all Mac computers come with bash), you may need to consider making your heavy lift in awk instead of pure bash:

 #!/bin/sh system_profiler SPUSBDataType \ | awk ' /Product ID:/{p=$3} /Vendor ID:/{v=$3} /Manufacturer:/{sub(/.*: /,""); m=$0} /Location ID:/{sub(/.*: /,""); printf("%s:%s %s (%s)\n", v, p, $0, m);} ' 

Or do not even wrap the shell completely with the shell:

 #!/usr/bin/awk -f BEGIN { while ("system_profiler SPUSBDataType" | getline) { if (/Product ID:/) {p=$3} if (/Vendor ID:/) {v=$3} if (/Manufacturer:/) {sub(/.*: /,""); m=$0} if (/Location ID:/) {sub(/.*: /,""); printf("%s:%s %s (%s)\n", v, p, $0, m)} } } 

Note that you can also get the output from system_profiler in XML format:

 $ system_profiler -xml SPUSBDataType 

To process this output, you will need an XML parser. And you will find that this is a lot of work parsing XML in native bash.

+15
source

You can use the system_profiler command, for example:

 system_profiler -detailLevel full 

and analyze the output. Or you can add the -xml option to the system_profiler command and easily parse the XML using awk / grep or the XML module in Perl.

Example:

  | | | | +-o FaceTime HD Camera (Built-in)@0 <class IOUSBInterface, id 0x1000002b2, registered, matched, active, busy 0 (26 ms), retain 7> | | | | | { | | | | | "IOCFPlugInTypes" = {"2d9786c6-9ef3-11d4-ad51-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"} | | | | | "bcdDevice" = 0x755 | | | | | "IOUserClientClass" = "IOUSBInterfaceUserClientV3" | | | | | "idProduct" = 0x850b | | | | | "bConfigurationValue" = 0x1 | | | | | "bInterfaceSubClass" = 0x1 | | | | | "locationID" = 0xfffffffffa200000 | | | | | "USB Interface Name" = "FaceTime HD Camera (Built-in)" | | | | | "idVendor" = 0x5ac 

As for the path to the USB device, I have no idea how you can do this simply on a Mac. I may be tempted to run:

 find /dev -type b -o -type c 

before inserting a USB device and saving the output. Then ask your user to insert the device and run the same command again to find out what additional device files were added as a result of connecting your device. Maybe rude, maybe effective - just an idea.

+3
source

Depending on what you want to do with the information, you can simply view it in System Information .

Click the Apple menu in the upper left corner of the screen, About this Mac , More Info , System Report and select Hardware in the upper left corner, then USB .

You may be able to write Applescript for this, but if you intend to interact with the device in some way, this may not be the best approach.

+2
source

All Articles