mirror of https://gitlab.com/hp3icc/emq-TE1.git
86 lines
2.5 KiB
Bash
86 lines
2.5 KiB
Bash
#!/bin/bash
|
|
# Nombre del script
|
|
SCRIPT_NAME="wifi-connect.sh"
|
|
|
|
# Registra el inicio en /opt/curl.txt
|
|
echo "Inicio: $SCRIPT_NAME" >> /opt/curl.txt
|
|
if [ "$(cat /proc/cpuinfo | grep 'Raspberry')" != "" ]; then
|
|
cd /opt
|
|
wget https://raw.githubusercontent.com/balena-os/wifi-connect/master/scripts/raspbian-install.sh
|
|
sed -i "s/CONFIRMATION=.*/CONFIRMATION=false/" raspbian-install.sh
|
|
chmod +x raspbian-install.sh
|
|
./raspbian-install.sh
|
|
rm raspbian-install.sh
|
|
#auto ap
|
|
sudo cat > /usr/local/bin/autoap.sh <<- "EOF"
|
|
#!/bin/bash
|
|
|
|
# Tiempo de espera inicial después del encendido en segundos
|
|
TIEMPO_ESPERA_INICIAL=120
|
|
|
|
# Lista de servicios a verificar y reiniciar si es necesario
|
|
SERVICIOS=("nginx" "hbmon.service" "hbmon2.service" "hbmon-js.service" "daprs-board.service")
|
|
|
|
# Esperar el tiempo inicial
|
|
sleep $TIEMPO_ESPERA_INICIAL
|
|
|
|
# Realizar prueba de ping a noip.com
|
|
if ping -c 1 noip.com >/dev/null 2>&1; then
|
|
# Si recibe respuesta, el script se detiene
|
|
echo "Respuesta de ping recibida. Saliendo del script."
|
|
exit 0
|
|
else
|
|
# Si no recibe respuesta, detener servicios, ejecutar wifi-connect y entrar en ciclo de ping
|
|
for servicio in "${SERVICIOS[@]}"; do
|
|
if sudo systemctl is-enabled "$servicio" >/dev/null 2>&1 && sudo systemctl is-active "$servicio" >/dev/null 2>&1; then
|
|
sudo systemctl stop "$servicio"
|
|
fi
|
|
done
|
|
|
|
wifi-connect &
|
|
|
|
# Ciclo infinito de ping hasta recibir respuesta
|
|
while true; do
|
|
if ping -c 1 noip.com >/dev/null 2>&1; then
|
|
# Recibió respuesta de ping, verificar y reiniciar servicios
|
|
for servicio in "${SERVICIOS[@]}"; do
|
|
if sudo systemctl is-enabled "$servicio" >/dev/null 2>&1 && ! sudo systemctl is-active "$servicio" >/dev/null 2>&1; then
|
|
sudo systemctl restart "$servicio"
|
|
fi
|
|
done
|
|
# Finalizar el script
|
|
echo "Respuesta de ping recibida. Saliendo del script."
|
|
exit 0
|
|
fi
|
|
# Esperar antes de realizar el siguiente intento de ping
|
|
sleep 5
|
|
done
|
|
fi
|
|
|
|
|
|
EOF
|
|
sudo chmod +x /usr/local/bin/autoap.sh
|
|
##
|
|
sudo cat > /lib/systemd/system/autoap.service <<- "EOF"
|
|
[Unit]
|
|
Description=AutoAP
|
|
After=network-pre.target
|
|
Wants=network-pre.target
|
|
|
|
[Service]
|
|
#ExecStartPre=/bin/sleep 60
|
|
ExecStart=/usr/local/bin/autoap.sh
|
|
Restart=on-failure
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
|
|
EOF
|
|
#
|
|
systemctl daemon-reload
|
|
systemctl enable autoap.service
|
|
|
|
fi
|
|
# Registra el final en /opt/curl.txt
|
|
echo "Finalizado: $SCRIPT_NAME" >> /opt/curl.txt
|