|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Function to list available USB devices with paths and names |
| 4 | +list_usb_devices() { |
| 5 | + # Create an array to store devices and their paths |
| 6 | + devices=() |
| 7 | + device_paths=() |
| 8 | + |
| 9 | + echo "Available USB devices with names and paths:" |
| 10 | + echo |
| 11 | + |
| 12 | + # Get the list of USB devices |
| 13 | + while IFS= read -r line; do |
| 14 | + # Extract bus and device number |
| 15 | + bus=$(echo "$line" | awk '{print $2}') |
| 16 | + device=$(echo "$line" | awk '{print $4}' | sed 's/://') |
| 17 | + |
| 18 | + # Use udevadm to find the corresponding sysfs path |
| 19 | + sysfs_path=$(udevadm info --query=path --name=/dev/bus/usb/$bus/$device 2>/dev/null) |
| 20 | + # Extract the device path using the port information |
| 21 | + device_path=$(echo "$sysfs_path" | grep -oE '[0-9]+-[0-9]+(.[0-9]+)*') |
| 22 | + |
| 23 | + # Debugging: Print extracted information |
| 24 | + echo "Debug: Bus=$bus, Device=$device, Sysfs Path=$sysfs_path, Device Path=$device_path" |
| 25 | + |
| 26 | + # If device path exists, add to the array |
| 27 | + if [ -n "$device_path" ]; then |
| 28 | + devices+=("$line Path: $device_path") |
| 29 | + device_paths+=("$device_path") |
| 30 | + fi |
| 31 | + done < <(lsusb) |
| 32 | + echo |
| 33 | + |
| 34 | + # Check if devices were found |
| 35 | + if [ ${#devices[@]} -eq 0 ]; then |
| 36 | + echo "No USB devices found." |
| 37 | + exit 1 |
| 38 | + fi |
| 39 | + |
| 40 | + echo "Select the USB device to restart by number:" |
| 41 | + echo |
| 42 | + |
| 43 | + # Display menu and handle user selection |
| 44 | + select choice in "${devices[@]}"; do |
| 45 | + if [ -n "$choice" ]; then |
| 46 | + DEVICE_PATH="${device_paths[$REPLY-1]}" |
| 47 | + break |
| 48 | + else |
| 49 | + echo "Invalid selection. Please try again." |
| 50 | + fi |
| 51 | + done |
| 52 | +} |
| 53 | + |
| 54 | +# Function to restart the USB device |
| 55 | +restart_usb() { |
| 56 | + local device_path=$1 |
| 57 | + echo "Unbinding USB device at path $device_path..." |
| 58 | + echo -n "$device_path" > /sys/bus/usb/drivers/usb/unbind |
| 59 | + |
| 60 | + # Sleep for a short duration to ensure the unbind completes |
| 61 | + sleep 1 |
| 62 | + |
| 63 | + echo "Rebinding USB device at path $device_path..." |
| 64 | + echo -n "$device_path" > /sys/bus/usb/drivers/usb/bind |
| 65 | + |
| 66 | + echo "USB device restart complete." |
| 67 | +} |
| 68 | + |
| 69 | +# Main script |
| 70 | +if [ $# -gt 0 ]; then |
| 71 | + # If arguments are passed, use them to restart the device |
| 72 | + for arg in "$@"; do |
| 73 | + echo "Restarting USB device at path $arg..." |
| 74 | + restart_usb "$arg" |
| 75 | + done |
| 76 | +else |
| 77 | + # If no arguments, use interactive selection |
| 78 | + list_usb_devices |
| 79 | + |
| 80 | + # Confirm with the user before restarting the USB device |
| 81 | + echo |
| 82 | + read -p "Are you sure you want to restart the USB device at path ${DEVICE_PATH}? (y/n) " confirm |
| 83 | + if [ "$confirm" = "y" ] || [ "$confirm" = "Y" ]; then |
| 84 | + restart_usb "$DEVICE_PATH" |
| 85 | + else |
| 86 | + echo "Operation cancelled." |
| 87 | + fi |
| 88 | +fi |
0 commit comments