Page 1 of 1
Capture Shell Command Output in Lua script
Posted: Monday 15 April 2019 12:38
by hendryman
from a lua script, i want to execute this shell command:
Code: Select all
hueadm lights | grep -q 'on' && echo 'lights_on'
if any of my hue lights is "on", this command will echo "lights_on".
my question: how i verify if "lights_on" was echoed back in lua? eg can os.execute() be used in a way that it captures the executed shell command's output?
Re: Capture Shell Command Output in Lua script
Posted: Monday 15 April 2019 14:12
by felix63
Would this work? Making a dummy switch and then curl the switch on from your shell script?
Code: Select all
hueadm lights | grep -q 'on' && curl 'http://DOMOTICZ-IP:8080/json.htm?type=command¶m=switchlight&idx=SWITCH_ID&switchcmd=On
Re: Capture Shell Command Output in Lua script
Posted: Monday 15 April 2019 21:13
by zicht
you could query the log anyway you want by this :
http://xxx.xxx.xxx.xxx:xxxx/json.htm?type=command¶m=getlog&lastlogtime=0&loglevel=268435455
This can be done by curl or shell whatever you want the respond will contain the switch name from log, since you invoke the command yourself you can assume the command is received. I use it to detect a reboot, and certain activities. With log lever you can select the details of log.
Hope it helps
Re: Capture Shell Command Output in Lua script
Posted: Monday 15 April 2019 21:40
by waaren
hendryman wrote: ↑Monday 15 April 2019 12:38
from a lua script, i want to execute this shell command:
Code: Select all
hueadm lights | grep -q 'on' && echo 'lights_on'
if any of my hue lights is "on", this command will echo "lights_on".
my question: how i verify if "lights_on" was echoed back in lua? eg can os.execute() be used in a way that it captures the executed shell command's output?
The attached function returns both the command output and the return code of the command.
Code: Select all
local function osExecute(cmd)
local fileHandle = assert(io.popen(cmd, 'r'))
local commandOutput = assert(fileHandle:read('*a'))
local returnTable = {fileHandle:close()}
return commandOutput,returnTable[3] -- rc[3] contains returnCode
end
Re: Capture Shell Command Output in Lua script
Posted: Tuesday 16 April 2019 9:58
by hendryman
thank you. much appreciated! i'm afraid my lua skills are very limited. could you please give an example on how to call this function and parse its results (three different parameters, if i understand correctly) from the main script? in my case, specifically, i'd like to receive the shell command's "matched" string (or the fact that the shell command did not return a string).
Re: Capture Shell Command Output in Lua script
Posted: Monday 22 April 2019 21:40
by deve87
I actually have the same question as you. I'm not any good in scripting. And was hoping someone know how to help with your/my question.
I got a script with io.popen work, but after like 2min. Domoticz lags bad and got error in log that 'Script has been running for more than 10sec'
I want to get a telnet session in Domoticz. And like I said, got it working but the problem.
Code: Select all
commandArray = {}
local s = os.date('%S')
if (s % 60 == 0) then
local f = assert(io.popen("telnet 192.168.10.110 20108 | strings &" , 'r'))
local Rotel = assert(f:read('*a'))
assert:close()
print(Rotel)
commandArray['UpdateDevice'] = '1376'..'|0|'.. Rotel ..''
end
return commandArray
Re: Capture Shell Command Output in Lua script
Posted: Monday 22 April 2019 22:05
by waaren
deve87 wrote: ↑Monday 22 April 2019 21:40
I actually have the same question as you. I'm not any good in scripting. And was hoping someone know how to help with your/my question.
I got a script with io.popen work, but after like 2min. Domoticz lags bad and got error in log that 'Script has been running for more than 10sec'
I want to get a telnet session in Domoticz. And like I said, got it working but the problem.
Code: Select all
commandArray = {}
local s = os.date('%S')
if (s % 60 == 0) then
local f = assert(io.popen("telnet 192.168.10.110 20108 | strings &" , 'r'))
local Rotel = assert(f:read('*a'))
assert:close()
print(Rotel)
commandArray['UpdateDevice'] = '1376'..'|0|'.. Rotel ..''
end
return commandArray
Not sure what you want to achieve but please understand that the eventsystem in domoticz is single threaded.
So all event triggered scripts will be waiting for this script to finish. The telnet command will likely not finish so that is the reason domoticz complains.
What might be an option here is to enter this commands in a bash script and execute that bash script in the background from Lua .
Re: Capture Shell Command Output in Lua script
Posted: Monday 22 April 2019 22:06
by waaren
hendryman wrote: ↑Tuesday 16 April 2019 9:58
thank you. much appreciated! i'm afraid my lua skills are very limited. could you please give an example on how to call this function and parse its results (three different parameters, if i understand correctly) from the main script? in my case, specifically, i'd like to receive the shell command's "matched" string (or the fact that the shell command did not return a string).
Happy to help if you can share your Lua script where you want to use this function.
Re: Capture Shell Command Output in Lua script
Posted: Monday 22 April 2019 22:07
by waaren
hendryman wrote: ↑Tuesday 16 April 2019 9:58
thank you. much appreciated! i'm afraid my lua skills are very limited. could you please give an example on how to call this function and parse its results (three different parameters, if i understand correctly) from the main script? in my case, specifically, i'd like to receive the shell command's "matched" string (or the fact that the shell command did not return a string).
Happy to help if you can share your Lua script where you want to use this function.
Re: Capture Shell Command Output in Lua script
Posted: Thursday 25 April 2019 22:24
by zicht
The above should work, but the url you are trying to read can indeed cause delays for many reasons.
You can use curl with the above instructions
Curl alows you to add timeouts
curl --connect-timeout 5 --max-time 9
that will avoid the 10 seconds message, and by tweaking it down to for example
curl --connect-timeout 2 --max-time 2
You have to test what works best for the url you are trying to reach
when the timeout is reached you get an empty string in return, but at least domoticz stops waiting so it does not "hang" waiting for your request to end.