Page 1 of 1
lua/dzVents script to capture output from os command
Posted: Thursday 03 May 2018 15:52
by snuiter
Hi,
need your help on lua/dZvent script to get output back in domoticz either to dummy switch/script to process.
I can check the tv status via domoticz raspberry(rPi1). TV is connected to remote raspberry pi(rPi2)
this works on the console but not via a script in domoticz.
Need some help to point me in the right direction.
This is the command I run on the command line
Code: Select all
ssh 192.168.x.x 'echo pow 0 | cec-client -s -d 1 | grep "power status:" | sed "s/..............//" '
output is either "on" or "standby"
Now I am trying with os.execute command in script, but not able to get the response from the command.
Any suggestion how to do that.
In a dZvent script I put these two line just for testing purposes
Code: Select all
local t = os.execute('ping -c 192.168.x.x')
print(t)
tried several other things but don't seem to be able to get the output I see from the same command on the console.
Any help is appreciated
Re: lua/dzVents script to capture output from os command
Posted: Thursday 03 May 2018 15:59
by jvdz
os.execute will only perform the command and return the return_code.
This should return the data of the cmd:
Code: Select all
cmd='ping -c 192.168.x.x'
local f = assert(io.popen(cmd, 'r'))
s = assert(f:read('*a'))
f:close()
print(s)
Jos
Re: lua/dzVents script to capture output from os command
Posted: Thursday 03 May 2018 16:46
by snuiter
Thanks for your quick response!
I will need to dive a bit deeper in these command not used them before.
Re: lua/dzVents script to capture output from os command
Posted: Sunday 27 May 2018 21:58
by snuiter
Hi Jos,
thanks for your response and I have tried many things, your example works when I execute a simple 'ping' or 'ls' command. But in my case I want to perform a command on remote rPi with ssh, like this 'ssh 192.168.2.214 'echo pow 0 | cec-client -s -d 1''. The response is nada zero nothing. If I perform this command on the command line I get this:
opening a connection to the CEC adapter...
power status: standby
I have tried many things but is this maybe a timing issue as from what I understand nothing is captured with this command. As said with the command ssh 192.168.2.214 'ls' I get as expected a directory listing.
Any suggestions?
Thx,
Ivo
- Spoiler: show
-
Code: Select all
function os.capture(cmd, raw) -- os.command uitvoeren en resultaat daarvan lezen in string
local f = assert(io.popen(cmd, 'r'))
local s = assert(f:read('*a'))
f:close()
print(s)
if raw then return s end
print(s)
s = string.gsub(s, '^%s+', '')
s = string.gsub(s, '%s+$', '')
s = string.gsub(s, '[{}]+', ' ')
s = string.gsub(s, '[\n\r]+', ' ')
return s
end
commandArray = {}
print('-----------script start------------')
cmd1='ping -c1 192.168.2.214'
cmd2='ls'
cmd3="ssh 192.168.2.214 'echo pow 0 | cec-client -s -d 1' "
cmd4='ssh 192.168.2.214 "ls" '
local command = "os.execute('ssh 192.168.2.214 "ls" ')"
content = os.capture(command,3)
print(command)
-- content = os.capture('ssh 192.168.2.214 "ls" ')
y=string.len(content)
print(content)
print(y)
print('--------------script end-------------')
return commandArray
Re: lua/dzVents script to capture output from os command
Posted: Monday 28 May 2018 0:21
by waaren
snuiter wrote: ↑Sunday 27 May 2018 21:58
... I want to perform a command on remote rPi with ssh, like this 'ssh 192.168.2.214 'echo pow 0 | cec-client -s -d 1''. The response is nada zero nothing.
Any suggestions?
I tried it on my system and got it working if I escape my quotes with a backslash.
Code: Select all
cmd="ssh pi-2 \"cat /var/log/domoticz.log | grep Bathroom | grep '16:00.232'\" "
local f = assert(io.popen(cmd, 'r'))
s = assert(f:read('*a'))
f:close()
print(s)