lua/dzVents script to capture output from os command

Post Reply
snuiter
Posts: 48
Joined: Saturday 17 June 2017 12:30
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

lua/dzVents script to capture output from os command

Post by snuiter » Thursday 03 May 2018 15:52

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

User avatar
jvdz
Posts: 1316
Joined: Tuesday 30 December 2014 20:25
Target OS: Raspberry Pi
Domoticz version: Stable
Location: Westland/Netherlands
Contact:

Re: lua/dzVents script to capture output from os command

Post by jvdz » Thursday 03 May 2018 15:59

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

snuiter
Posts: 48
Joined: Saturday 17 June 2017 12:30
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: lua/dzVents script to capture output from os command

Post by snuiter » Thursday 03 May 2018 16:46

Thanks for your quick response!

I will need to dive a bit deeper in these command not used them before.

snuiter
Posts: 48
Joined: Saturday 17 June 2017 12:30
Target OS: Raspberry Pi
Domoticz version: beta
Contact:

Re: lua/dzVents script to capture output from os command

Post by snuiter » Sunday 27 May 2018 21:58

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

User avatar
waaren
Posts: 613
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: lua/dzVents script to capture output from os command

Post by waaren » Monday 28 May 2018 0:21

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)
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave , dzVents 2.4.n, Amazon echo

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests