Home > Linux > Ubuntu: USB Power Switching Script

Ubuntu: USB Power Switching Script

Non è strettamente necessario togliere alimentazione al dispositivo USB, l’importante è sempre Smontarlo.
Nel caso si avesse la paranoia, durante la rimozione, vedendo comunque il led del dispositivo acceso, eseguire lo script di switchoff qui di seguito.

#!/bin/bash

# USB Power Switching Script
# Script to toggle power management status of USB devices
# by DanyTheBat

declare -a ARRAYDEV
declare -a ARRAYPROD
declare -a ARRAYPWRSTATUS
declare -a ARRAYMAXPWR
let nprod=0

function padl {
    tmp=$2
    while [ ${#tmp} -lt $1 ];
    do
        tmp="$tmp "
    done
    echo "$tmp"
}

function padr {
    tmp=$2
    while [ ${#tmp} -lt $1 ];
    do
        tmp=" $tmp"
    done
    echo "$tmp"
}

function lower {
    echo "$(echo $1 | tr [:upper:] [:lower:])"
}

function query_level {
    # level: on - auto - suspend

    if [ -e $1/power/level ]; then
        ARRAYPWRSTATUS[$nprod]=$(cat $devhome/power/level)
    else if [ ! -e $devhome/power/level ]; then
        ARRAYPWRSTATUS[$nprod]=NA
        fi
    fi
}

function scan_usb {
    for idVendor in $(ls /sys/bus/usb/devices/*/idVendor); do
        devhome=$(dirname $idVendor)
        devlevel=$devhome/power/level

        # if product, leo, else leo idvendor e idproduct
        if [ -e $devhome/product ]; then
            ARRAYPROD[$nprod]="$devhome"
            ARRAYPRODDESC[$nprod]="$(cat $devhome/product)"
            ARRAYMAXPWR[$nprod]="$(cat $devhome/bMaxPower)"
            query_level $devhome
            ((nprod++))
        else if [ -e $devhome/idVendor ]; then
            ARRAYPROD[$nprod]="$devhome"
            ARRAYPRODDESC[$nprod]="$(cat $devhome/idVendor):$(cat $devhome/idProduct)"
            ARRAYMAXPWR[$nprod]="$(cat $devhome/bMaxPower)"
            query_level $devhome
            ((nprod++))
            fi
        fi
    done
}

function list_usb {
    for element in $(seq 0 $((${#ARRAYPROD[@]} -1))); do
        echo -e "|$(padl 25 "${ARRAYPRODDESC[$element]}")|$(padl 30 "(${ARRAYPROD[$element]})")|$(padr 11 "[${ARRAYPWRSTATUS[$element]}]")|$(padr 7 "${ARRAYMAXPWR[$element]}")|"
        #echo -e "${ARRAYPRODDESC[$element]}\\t(${ARRAYPROD[$element]})\\t[${ARRAYPWRSTATUS[$element]}]\\t${ARRAYMAXPWR[$element]}"
    done
}

function toggle_usb {
    for element in $(seq 0 $((${#ARRAYPROD[@]} -1))); do
        if [[ $(lower "${ARRAYPRODDESC[$element]}") =~ $(lower "$1") ]]; then
            if [ "${ARRAYPWRSTATUS[$element]}" == "on" ] || [ "${ARRAYPWRSTATUS[$element]}" == "auto" ] ; then
                echo "suspend" > ${ARRAYPROD[$element]}/power/level
                echo -e "${ARRAYPRODDESC[$element]} switching from [${ARRAYPWRSTATUS[$element]}] to [suspend]"
            else
                echo "auto" > ${ARRAYPROD[$element]}/power/level
                echo -e "${ARRAYPRODDESC[$element]} switching from [${ARRAYPWRSTATUS[$element]}] to [auto]"
            fi
        fi
    done
}

function verify_usb {
    for element in $(seq 0 $((${#ARRAYPROD[@]} -1))); do
        if [[ $(lower "${ARRAYPRODDESC[$element]}") =~ $(lower "$1") ]]; then
            return 0
        fi
    done

    return 1
}

function get_usb_by_name {
    for element in $(seq 0 $((${#ARRAYPROD[@]} -1))); do
        if [[ $(lower "${ARRAYPRODDESC[$element]}") =~ $(lower "$1") ]]; then
            echo -e "|$(padl 25 "${ARRAYPRODDESC[$element]}")|$(padl 30 "(${ARRAYPROD[$element]})")|$(padr 11 "[${ARRAYPWRSTATUS[$element]}]")|$(padr 7 "${ARRAYMAXPWR[$element]}")|"
        fi
    done
}

scan_usb

case $1 in
    -l) echo -e
        echo -e "USB Power Switching Script"
        echo -e "------------------------------------------------------------------------------"
        list_usb
        echo -e "------------------------------------------------------------------------------"
        echo -e
    ;;

    -t) if [ ! "$2" == "" ] && verify_usb $2; then
        toggle_usb $2
        else
        echo "Specify a valid USB Device: '$2' doesn't exist!!!"
        fi
    ;;

    -v) if verify_usb $2; then
        echo -e
        echo -e "USB Power Switching Script"
        echo -e "------------------------------------------------------------------------------"
        get_usb_by_name $2
        echo -e "------------------------------------------------------------------------------"
        echo -e
        echo -e "'$2' USB Device exists!!!"
        echo -e
        else
        echo -e
        echo -e "USB Power Switching Script"
        echo -e
        echo -e "'$2' USB Device doesn't exist!!!"
        echo -e
        fi
    ;;

    *) echo -e
       echo -e "USB Mouse Power Switching Script"
       echo -e "------------------------------------------------------------------------------"
       echo -e "-l\\tList USB Devices"
       echo -e "-t name\\ttoggle USB Device 'name' status"
       echo -e "-v name\\tverify if USB Device 'name' exists"
       echo -e
    ;;
esac

Si ringrazia DanyTheBat.
Fonte.

Categorie:Linux
  1. Non c'è ancora nessun commento.
  1. No trackbacks yet.

Lascia un commento

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione / Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione / Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione / Modifica )

Google+ photo

Stai commentando usando il tuo account Google+. Chiudi sessione / Modifica )

Connessione a %s...

%d blogger cliccano Mi Piace per questo: