Skip to content

Instantly share code, notes, and snippets.

@unixbigot
Created August 15, 2023 00:44
Show Gist options
  • Save unixbigot/37406dfb47b8d33fd9fdff98c0a2d3a9 to your computer and use it in GitHub Desktop.
Save unixbigot/37406dfb47b8d33fd9fdff98c0a2d3a9 to your computer and use it in GitHub Desktop.
A script for MacOS that provides a brief summary of USB devices similar to Linux lsusb
#!/usr/bin/perl
#
# This script for MacOS gives a nice succinct list of all the devices on
# the USB bus, similar to the output of the Linux "lsusb" program.
#
use strict;
use warnings;
use v5.30;
use JSON;
use Data::Dumper qw(Dumper);
my $busitems = decode_json(`system_profiler -json SPUSBDataType 2>/dev/null`);
#say "Bus items are",Dumper($busitems);
sub print_item {
my ($indent, $item)=@_;
#say "Bus item ",Dumper($item);
print "${indent}";
print("$1 ") if defined($$item{vendor_id}) && $$item{vendor_id}=~m/\((.*)\)/;
print $$item{_name};
print(" ",(split / /,$$item{vendor_id},2)[0],":",$$item{product_id}) if $$item{vendor_id};
print("\n");
if ($item->{_items}) {
print_item($indent." ", $_) for @{$item->{_items}};
}
}
print_item('', $_) foreach (@{$$busitems{SPUSBDataType}});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment