Skip to content

Instantly share code, notes, and snippets.

@tienntr
Created November 15, 2024 06:56
Show Gist options
  • Save tienntr/9b3cabd5146126751be128f0b910bc5d to your computer and use it in GitHub Desktop.
Save tienntr/9b3cabd5146126751be128f0b910bc5d to your computer and use it in GitHub Desktop.
List ttyUSB devices and their properties
#!/bin/bash
for device in /dev/ttyUSB*; do
if [ -e "$device" ]; then
# Get the device path from udevadm
dev_path=$(udevadm info -q path -n "$device")
# Construct the full path
full_path="/sys$dev_path"
# Traverse upwards to find the actual USB device node
usb_path=$(dirname "$(readlink -f "$full_path")")
while [ ! -f "$usb_path/busnum" ] && [ "$usb_path" != "/" ]; do
usb_path=$(dirname "$usb_path")
done
# Extract the bus and device numbers if found
if [ -f "$usb_path/busnum" ] && [ -f "$usb_path/devnum" ]; then
bus_number=$(cat "$usb_path/busnum")
device_number=$(cat "$usb_path/devnum")
# Get device information: Vendor ID, Product ID, Vendor name, Product name, and Serial
id_vendor=$(cat "$usb_path/idVendor")
id_product=$(cat "$usb_path/idProduct")
vendor_name=$(udevadm info -q property --property=ID_VENDOR_FROM_DATABASE --value -p "$usb_path")
product_name=$(udevadm info -q property --property=ID_MODEL_FROM_DATABASE --value -p "$usb_path")
serial_number=$(cat "$usb_path/serial")
product_string=$(cat "$usb_path/product")
manufacturer=$(cat "$usb_path/manufacturer")
# Format and display the output
echo "$device: bus $bus_number device $device_number VID $id_vendor ($vendor_name) PID $id_product ($product_name)"
echo "Manufacturer: $manufacturer"
echo "Product: $product_string"
echo "Serial: $serial_number"
echo
else
echo "Unable to find USB path for device: $device"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment