Skip to content

Instantly share code, notes, and snippets.

@LuisPalacios
Created March 7, 2023 11:16
Show Gist options
  • Save LuisPalacios/8ff7a2d289d115a97969faa1788e7367 to your computer and use it in GitHub Desktop.
Save LuisPalacios/8ff7a2d289d115a97969faa1788e7367 to your computer and use it in GitHub Desktop.
Activar USB Dongle TP-Link UE300 GigabitEthernet en Raspberry Pi OS tras reboot
#!/bin/bash
## Con un par de Pi 4B v1.5 tengo problemas con el dispositivo
## TP-Link UE300 10/100/1000 LAN (ethernet mode) [Realtek RTL8153]
##
## Funciona perfectamente, pero al hacer reboot no lo activa. Sí que
## aparece en el bus USB pero no lo activa, por lo que el device
## eth1 no se me activa en el servidor.
##
## Este script lo soluciona :-)
## Es un script quizá demasiado complejo pero hice muchas pruebas y
## al final lo dejo así (con tanto cálculo) para tenerlo como referencia.
##
## Un caso de uso es en el proyecto siguiente:
## https://www.luispa.com/linux/2014/10/19/bridge-ethernet.html
##
##
## Logs
##
export logfile="/var/log/pi_eth1_up.log"
dry_echo() {
echo "${1}" >> ${logfile}
}
echo "-- ${0}" > ${logfile}
date >> ${logfile}
## Preparo las variables
##
ifEth="eth1" # Device eth? donde espero encontrar al USB dongle
sString="tp-link" # String para bucar el dongle
ifEthPresent=1 # Device is "not" present...
## Adivino el Bus/Device de mi dongle USB Ethernet
##
Bus=`lsusb | grep -i ${sString} | awk 'match($0,/Bus [^ ]*/){ print substr($0, RSTART,RLENGTH)}' | awk '{print $2}' 2>/dev/null`
Device=`lsusb | grep -i ${sString} | awk 'match($0,/Device [^ ]*/){ print substr($0, RSTART,RLENGTH)}' | awk '{print $2}' | sed 's/://' 2>/dev/null`
ID=`lsusb | grep -i ${sString} | awk 'match($0,/ID [^ ]*/){ print substr($0, RSTART,RLENGTH)}' | awk '{print $2}' 2>/dev/null`
idVendor=`echo ${ID} | awk '{split($0,a,":"); print a[1]}' 2>/dev/null`
idProduct=`echo ${ID} | awk '{split($0,a,":"); print a[2]}' 2>/dev/null`
## Si no encuentro el dongle me piro
if [ "${Bus}" = "" ] || [ "${Device}" = "" ]; then
dry_echo "No encuentro el device ${sString}, me piro..."
exit
fi
dry_echo "Encontré el Device ${sString}: ${Bus}/${Device} - ${idVendor}:${idProduct}"
# Intento n veces activar el dongle.
# Pner {1..3} para tres veces, solo intento 1 vez.
for run in {1..1}; do
# Si está activo me piro !
ip a show dev ${ifEth} >/dev/null 2>&1
ifEthPresent=${?}
if [ "${ifEthPresent}" == "0" ]; then
exit
fi
# No está activo, intento hacer un reset del USB Dongle
dry_echo "> usbreset ${Bus}/${Device}"
dry_echo "> usb_modeswitch -v ${idVendor} -p ${idProduct} -R"
usb_modeswitch -v ${idVendor} -p ${idProduct} -R
dry_echo "--"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment