From 49aa85ba406dafc7202927bad200179551644b9a Mon Sep 17 00:00:00 2001 From: Giorgio Ravera Date: Sat, 27 Mar 2021 21:54:24 +0100 Subject: [PATCH] Improved shelly bash scripts --- shell_scripts/shelly_reboot.sh | 10 ++++++--- shell_scripts/shelly_upgrade.sh | 6 +++++- shell_scripts/shelly_uptime.sh | 38 +++++++++++++++++++++++++++++++++ shell_scripts/shelly_version.sh | 18 +++++++++++----- 4 files changed, 63 insertions(+), 9 deletions(-) create mode 100755 shell_scripts/shelly_uptime.sh diff --git a/shell_scripts/shelly_reboot.sh b/shell_scripts/shelly_reboot.sh index 3a7be61f..e9206831 100755 --- a/shell_scripts/shelly_reboot.sh +++ b/shell_scripts/shelly_reboot.sh @@ -1,6 +1,10 @@ #!/bin/bash -HASS_HOME="/config" +if [ -f /.dockerenv ]; then + HASS_HOME="/config" +else + HASS_HOME="../" +fi USER=$(awk '/shelly_user/ { print $2 }' $HASS_HOME/secrets.yaml) PASSWORD=$(awk '/shelly_password/ { print $2 }' $HASS_HOME/secrets.yaml) SHELLY_ID=( @@ -26,8 +30,8 @@ for i in "${SHELLY_ID[@]}" do ALIVE=$(ping -c 1 $i |grep ttl) if [ ! -z "$ALIVE" ]; then - STATUS=$(curl -s --user $USER:$PASSWORD http://$i/reboot | sed -e "s/{"ok"}://g") - echo $STATUS + STATUS=$(curl -s --user $USER:$PASSWORD http://$i/reboot | jq -r .ok) + echo $i: $STATUS fi done diff --git a/shell_scripts/shelly_upgrade.sh b/shell_scripts/shelly_upgrade.sh index 3a1c1d6b..1de2f651 100755 --- a/shell_scripts/shelly_upgrade.sh +++ b/shell_scripts/shelly_upgrade.sh @@ -1,6 +1,10 @@ #!/bin/bash -HASS_HOME="/config" +if [ -f /.dockerenv ]; then + HASS_HOME="/config" +else + HASS_HOME="../" +fi HOST=$(awk '/mqtt_host/ { print $2 }' $HASS_HOME/secrets.yaml) USER=$(awk '/mqtt_user/ { print $2 }' $HASS_HOME/secrets.yaml) PASSWORD=$(awk '/mqtt_password/ { print $2 }' $HASS_HOME/secrets.yaml) diff --git a/shell_scripts/shelly_uptime.sh b/shell_scripts/shelly_uptime.sh new file mode 100755 index 00000000..fcf33cd5 --- /dev/null +++ b/shell_scripts/shelly_uptime.sh @@ -0,0 +1,38 @@ +#!/bin/bash + +if [ -f /.dockerenv ]; then + HASS_HOME="/config" +else + HASS_HOME="../" +fi +USER=$(awk '/shelly_user/ { print $2 }' $HASS_HOME/secrets.yaml) +PASSWORD=$(awk '/shelly_password/ { print $2 }' $HASS_HOME/secrets.yaml) +SHELLY_ID=( + shelly1.giorgioravera.it + shelly2.giorgioravera.it + shelly3.giorgioravera.it + shelly4.giorgioravera.it + shelly5.giorgioravera.it + shelly6.giorgioravera.it + shelly7.giorgioravera.it + shelly8.giorgioravera.it + shelly9.giorgioravera.it + shelly10.giorgioravera.it + shelly11.giorgioravera.it + shellybulb.giorgioravera.it + shellyem.giorgioravera.it + shellyplugs.giorgioravera.it +) + +echo "Check Shelly Firmware" + +for i in "${SHELLY_ID[@]}" +do + ALIVE=$(ping -c 1 $i |grep ttl) + if [ ! -z "$ALIVE" ]; then + UPTIME=$(curl -s --user $USER:$PASSWORD http://$i/status | jq -r .uptime) + echo $i: $UPTIME + fi +done + +echo "Done" diff --git a/shell_scripts/shelly_version.sh b/shell_scripts/shelly_version.sh index b1d2c2ef..0e64d479 100755 --- a/shell_scripts/shelly_version.sh +++ b/shell_scripts/shelly_version.sh @@ -1,6 +1,10 @@ #!/bin/bash -HASS_HOME="/config" +if [ -f /.dockerenv ]; then + HASS_HOME="/config" +else + HASS_HOME="../" +fi USER=$(awk '/shelly_user/ { print $2 }' $HASS_HOME/secrets.yaml) PASSWORD=$(awk '/shelly_password/ { print $2 }' $HASS_HOME/secrets.yaml) SHELLY_ID=( @@ -26,10 +30,14 @@ for i in "${SHELLY_ID[@]}" do ALIVE=$(ping -c 1 $i |grep ttl) if [ ! -z "$ALIVE" ]; then - VERSION=$(curl -s --user $USER:$PASSWORD http://$i/status | sed -e "s/},/}\n/g" |grep \"update\" | sed -e "s/,/\n/g" | sed -e "s/:/:\t/g" | awk '/has_update/ { print $2 }') - NEW=$(curl -s --user $USER:$PASSWORD http://$i/status | sed -e "s/},/}\n/g" |grep \"update\" | sed -e "s/,/\n/g" | sed -e "s/:/:\t/g" | awk '/new_version/ { print $2 }') - OLD=$(curl -s --user $USER:$PASSWORD http://$i/status | sed -e "s/},/}\n/g" |grep \"update\" | sed -e "s/,/\n/g" | sed -e "s/:/:\t/g" | awk '/old_version/ { print $2 }') - echo $i: $VERSION - $OLD - $NEW + HAS_UPDATE=$(curl -s --user $USER:$PASSWORD http://$i/status | jq -r .update.has_update) + NEW=$(curl -s --user $USER:$PASSWORD http://$i/status | jq -r .update.new_version) + OLD=$(curl -s --user $USER:$PASSWORD http://$i/status | jq -r .update.old_version) + if [ $HAS_UPDATE == "true" ]; then + echo $i: version: $OLD - new version: $NEW + else + echo $i: version: $OLD - new version: N/A + fi fi done -- 2.47.3