Skip to content

Instantly share code, notes, and snippets.

@parrotmac
Created December 11, 2019 08:13
Show Gist options
  • Select an option

  • Save parrotmac/e448bc29ccd012f3f331ec4d2c00ace0 to your computer and use it in GitHub Desktop.

Select an option

Save parrotmac/e448bc29ccd012f3f331ec4d2c00ace0 to your computer and use it in GitHub Desktop.
Script to make identifying physical devices plugged into Raspberry Pi 3 B+ USB ports easier
#!/usr/bin/env bash
#
# List Ports - Raspberry Pi 3 B+ USB Port Identifier
# Author: Isaac Parker <parrotmac@gmail.com>
# License: MIT
#
# Script to make identifying physical devices
# plugged into Raspberry Pi USB ports easier
# sys path for USB devices
BASE_PATH="/sys/bus/usb/devices/"
# USB Port identifiers
# As viewed from port end (w/ Ethernet on the left)
# Top Left; Top Right; Bottom Left; Bottom Right
PORT_TL="1-1.1.2"
PORT_TR="1-1.3"
PORT_BL="1-1.1.3"
PORT_BR="1-1.2"
FILE_PID="/idProduct"
FILE_VID="/idVendor"
FILE_MFG="/manufacturer"
get_device_info_if_exists() {
local port_id="$1"
local device_path="${BASE_PATH}${port_id}"
local pid=$(cat "${device_path}${FILE_PID}" 2>/dev/null)
local vid=$(cat "${device_path}${FILE_VID}" 2>/dev/null)
local mfg=$(cat "${device_path}${FILE_MFG}" 2>/dev/null)
printf "${mfg}\n${vid}:${pid}"
}
main() {
# Fetch info for all ports
INFO_TL=$(get_device_info_if_exists "${PORT_TL}")
INFO_TR=$(get_device_info_if_exists "${PORT_TR}")
INFO_BL=$(get_device_info_if_exists "${PORT_BL}")
INFO_BR=$(get_device_info_if_exists "${PORT_BR}")
# Print a verbose 'table'
echo "-------------------------------------"
echo "TL: ${INFO_TL}"
echo "-------------------------------------"
echo "TR: ${INFO_TR}"
echo "-------------------------------------"
echo "BL: ${INFO_BL}"
echo "-------------------------------------"
echo "BR: ${INFO_BR}"
echo "-------------------------------------"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment