From 4761bfea0f4e10064ef3c54fa7a404f0ce544ab5 Mon Sep 17 00:00:00 2001 From: Ratatoskr Date: Fri, 22 Dec 2023 15:07:05 +0100 Subject: [PATCH] Refactor Wecker Script for Improved Readability and Functionality - Combined IP retrieval and ping test into a single function 'check_ip'. - Improved error handling for better script robustness. - Removed unnecessary commented-out code and added comments for clarity. - Ensured consistency in indentation and formatting. Author: Michael, Haider Date: 22.12.2023 --- wecker.sh | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/wecker.sh b/wecker.sh index d42fc69..952678c 100755 --- a/wecker.sh +++ b/wecker.sh @@ -39,11 +39,32 @@ set_color() { iobroker state set zigbee.0.04cd15fffee03198.color "$color_value" } -# Die IP-Adresse von Lisas-IPhone holen. +# Die IP-Adresse von Lisas-IPhone holen und Ping-Test durchführen check_ip() { mac_address="e4:cd:d1" # Ersetze dies durch die MAC-Adresse A72_ip=$(arp-scan --localnet | grep "$mac_address" | awk '{print $1}') + + if [ -z "$A72_ip" ]; then + echo "IP nicht gefunden. Ziel ist nicht erreichbar." + return 1 + fi + echo "Gefundene IP: $A72_ip" + + # Ping-Test durchführen + echo "teste ping $A72_ip" + local ping_output=$(/bin/ping -q -w 3 -c 3 $A72_ip 2>&1) + local ping_exit_code=$? + + echo "$ping_output" # Ausgabe des Ping-Befehls anzeigen + + if [ $ping_exit_code -eq 0 ] && [[ "$ping_output" == *"3 received"* ]]; then + echo "Das Ziel ist erreichbar." + return 0 # Erfolg: Das Ziel ist erreichbar + else + echo "Das Ziel ist nicht erreichbar. Exit Code: $ping_exit_code" + return 1 # Fehler: Das Ziel ist nicht erreichbar + fi } # check_ip -Funktion ausführen. @@ -70,6 +91,7 @@ check_notification() { for ((i = 1; i <= $max_attempts; i++)); do # Führe den SSH-Befehl innerhalb eines Timeout aus + check_ip # IP holen output=$(ssh -i /root/.ssh/A72 root@$A72_ip 'dumpsys activity processes | grep com.urbandroid.sleep') echo $output @@ -140,13 +162,13 @@ colortemp_step=$(bc <<< "$start_colortemp / $steps") set_brightness "$current_brightness" set_colortemp "$current_colortemp" - sleep 27 # Änderungen alle 30 (27) Sekunden + sleep 30 # Änderungen alle 30 Sekunden done - # check_ip -Funktion ausführen. check_ip echo "$A72_ip" + # Starten des Alarm-Scripts auf dem Handy. ssh -i /root/.ssh/A72 root@$A72_ip '/data/scripts/morning-alarm.sh' > /root/morning_alarm.log