0 votos

Ejecución diferente de bash script en la acción 'execute shell script' en Keyboard Maestro.

Intento ejecutar shell script utilizando Keyboard Maestro. En MacOS Terminal y Raspberry todo funciona bien. KM muestra un extraño error. Parece que el script funciona pero reconoce mal los caracteres o algo así. La contraseña guardada en sha1 es correcta.

KM con error:

Terminal con resultado correcto en este mismo dispositivo:

script (funciona en todos los dispositivos):

#!/bin/bash
# ------------------------------------------------------------------------------------------
# This script mimics the usage of the Home Wizard Lite app
# ------------------------------------------------------------------------------------------
# Dependencies: curl and jq (sudo apt-get install curl jq)
# It needs three parameters:
# - the SmartSwitch you want to control, between quotes (exactly as named in the Home Wizard Lite app)
# - the device you want to control, between quotes (exactly as named in the Home Wizard Lite app)
# - the action you want to perform, between quotes
# Depending on the device you control, one of the following actions may apply:
actionlist="On, Off, Up, Down, Left, Right, Stop, Favorite, Pair, ManualMode, AutomaticMode, DayMode, NightMode, GetState, Range, Open, Close"
# The fourth parameter is optional and indicates the time (in seconds) that the script will keep trying to perform the action.
# Example call: ./send_homewizard.sh "SmartSwitch1" "Controller1" "On" 60
# ------------------------------------------------------------------------------------------
# You have to fill in your HomeWizard Lite username and the sha1-hash of your password between the quotes:
username="mymail@gmail.com"
password_sha1="4bg7j41b3785d06465b507790z09drge96414386" #can be generated on https://hash.online-convert.com/sha1-generator
# ------------------------------------------------------------------------------------------

# Checking the parameters
# -----------------------------------
searchswitch=$1 # e.g. SmartSwitch1
if [ "$searchswitch" = "" ]; then echo -e "Missing parameter SmartSwitch ...\nExiting ..."; exit; fi
searchdevice=$2 # e.g. Controller1
if [ "$searchdevice" = "" ]; then echo -e "Missing parameter Device ...\nExiting ..."; exit; fi
doaction=$3 # On, Off, or one of the other applicable actions
if [ "$doaction" = "" ]; then echo -e "Missing parameter Action ...\nExiting ..."; exit; fi
if [[ ! ", $actionlist, " = *", $doaction, "* ]]; then echo -e "Action not in list {$actionlist}\nExiting ..."; exit; fi
timeout=$4 # in seconds (default 10)
if ! [[ "$timeout" =~ ^[0-9]+$ ]]; then timeout=10; fi
echo "Sending {"$searchswitch", "$searchdevice", "$doaction"} during max. "$timeout" seconds ..."

# Login to HomeWizard cloud
# -----------------------------------
login=$(curl -sS -u $username:$password_sha1 "https://cloud.homewizard.com/account/login")
#echo $login
if [[ ! "$(echo $login | jq -r '.status')" = "ok" ]]; then
   echo -e "Login failed ... Did you enter correctly your username and password_sha1 in the script?\nExiting ..."
   exit
fi
sessionid=$(echo $login | jq -r '.session')
#echo $sessionid

# Determining the plugid and deviceid
# -----------------------------------
alljson=$(curl -sS -H "X-Session-Token: $sessionid" "https://plug.homewizard.com/plugs")
#echo $alljson
plugid=$(echo $alljson | jq --arg ss $searchswitch -r 'select(.[].name==$ss) | .[].id')
#echo $plugid
if [ "$plugid" = "" ]; then
   echo -e "$searchswitch not found ... Is the name exactly as in the app?\nExiting ..."
   exit
fi
devices=$(echo $alljson | jq --arg ss $searchswitch 'select(.[].name==$ss) | .[].devices')
#echo $devices
deviceid=$(echo $devices | jq --arg sd $searchdevice -r '.[] | select(.name==$sd) | .id')
#echo $deviceid
if [ "$deviceid" = "" ]; then
   echo -e "$searchdevice not found ... Is the name exactly as in the app?\nExiting ..."
   exit
fi

# Sending the action
# -----------------------------------
startsec=$SECONDS
#echo $startsec
endsec=$(($startsec+$timeout))
#echo $endsec
while [ $SECONDS -lt $endsec ] ; do
      status=$(curl -sS -H "X-Session-Token: $sessionid" -H "Content-Type: application/json; charset=utf-8" -X POST -d '{"action": "'$doaction'"}' 'https://plug.homewizard.com/plugs/'$plugid'/devices/'$deviceid'/action')
      echo $status
      if [ "$status" = "{\"status\":\"Success\"}" ]; then
         break
      fi
done

1 votos

Puedes cambiar el script para que muestre el valor completo de $login después de la primera llamada a curl ¿Y añadirlo a la pregunta? Básicamente "quitar el # antes de echo $login Vuelva a ejecutarlo y actualice la salida que obtiene de Keyboard Maestro".

4 votos

¿Has probado un Ejemplo mínimo, completo y verificable ? ¿Puede simplificar el script para determinar exactamente qué líneas están causando el problema? Se trata de un script muy largo con todo tipo de contenido.

0 votos

Scripts en el terminal tienen la ventaja de lo que se haya hecho en tu perfil. Dudo que los que se ejecutan desde Keyboard Maestro lo hagan. Crea un nuevo usuario. Intente ejecutar los scripts desde allí. añada comandos de depuración a su scripts pwd >>mylog.txt; id >>mylog.txt

2voto

geocoin Puntos 121

Dado que la cuestión está en algún lugar de aquí:

[[ ! "$(echo $login | jq -r '.status')" = "ok" ]]

Supongo que jq no está en la ruta del entorno con el que se está ejecutando KM.

Intente utilizar la ruta completa.

En los documentos de KM me respaldan :

Ruta en Shell scripts
En esencia, la ruta por defecto en un Keyboard Maestro Execute Shell script es la ruta base para el sistema:

Cambia jq por una ruta completa, a ver qué pasa. Si no sabes dónde está instalado jq, entonces hazlo:

which jq

AppleAyuda.com

AppleAyuda es una comunidad de usuarios de los productos de Apple en la que puedes resolver tus problemas y dudas.
Puedes consultar las preguntas de otros usuarios, hacer tus propias preguntas o resolver las de los demás.

Powered by:

X