dzVents os.execute not executing command [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

dzVents os.execute not executing command [Solved]

Post by MikeF »

Hi,

I'm running Domoticz (v4.9700) on a RPi, and I'm trying to control powered speakers on a separate RPi using an mpc playlist.

It works if I run the following at the command line:

Code: Select all

sshpass -p <password> ssh pi@<second RPi url> mpc play 1
I've created a selector switch and the following dzVents script:

Code: Select all

--
-- This script plays a streaming radio URL when the device is changed
-- It passes an mpc command to another RPi using sshpass and ssh
--

return {
	on = {
		devices = { 'Kitchen radio' }
	},
	execute = function(dz)
	
	local level   = dz.devices("Kitchen radio").rawData[1]
	level = level / 10
	local base = "sshpass -p <password> ssh pi@<second RPi url> mpc play "
		
	if level >= 1 and level <= 7 then
		cmd = base .. level
		print (cmd)
		os.execute(cmd)
	
	end
	
end

}
However, this appears to do nothing! (I can run the command displayed in the log at the command line OK.)
Last edited by MikeF on Wednesday 28 November 2018 19:36, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents os.execute not executing command

Post by waaren »

MikeF wrote: Thursday 22 November 2018 10:07 Hi,
I'm running Domoticz (v4.9700) on a RPi, and I'm trying to control powered speakers on a separate RPi using an mpc playlist.
It works if I run the following at the command line:

Code: Select all

sshpass -p <password> ssh pi@<second RPi url> mpc play 1
Can you try with this. It should display what is happening and the returnCode of you command.

Code: Select all

--
-- This script plays a streaming radio URL when the device is changed
-- It passes an mpc command to another RPi using sshpass and ssh
--

return {
    on = { devices = { 'Kitchen radio' }},
    
    logging =   {   level      =   domoticz.LOG_DEBUG,
                    marker     =   "sshPass"    },
                    

    execute = function(dz, item, info )
        
        _G.logMarker             = info.scriptName                  -- sets the logmarker for dz.log
        
        local function logWrite(str,level)
            dz.log(str,level or dz.LOG_DEBUG)
        end

        local function osExecute(cmd)
            local fileHandle     = assert(io.popen(cmd, 'r'))
            local commandOutput  = assert(fileHandle:read('*a'))
            local returnTable    = {fileHandle:close()}
            
            logWrite(commandOutput)
            logWrite(returnTable[3])
           return commandOutput,returnTable[3]            -- rc[3] contains returnCode
        end
        
        local level   = dz.devices("Kitchen radio").rawData[1]
        level = level / 10
        local base = "sshpass -p <password> ssh pi@<second RPi url> mpc play "

        if level >= 1 and level <= 7 then
            cmd = base .. level
            osExecute(cmd)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: dzVents os.execute not executing command

Post by MikeF »

Thanks, waaren.

This is what I get:

Code: Select all

2018-11-22 18:54:29.098 Status: User: Admin initiated a switch command (276/Kitchen radio/Set Level)
2018-11-22 18:54:29.609 Status: dzVents: Info: sshPass: ------ Start external script: kitchen_radio2.lua: Device: "Kitchen radio (Dummy)", Index: 276
2018-11-22 18:54:29.890 Status: dzVents: Debug: kitchen_radio2:
2018-11-22 18:54:29.890 Status: dzVents: Debug: kitchen_radio2: 6
2018-11-22 18:54:29.890 Status: dzVents: Info: kitchen_radio2: ------ Finished kitchen_radio2.lua 
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents os.execute not executing command

Post by waaren »

MikeF wrote: Thursday 22 November 2018 20:03 Thanks, waaren.

This is what I get:

Code: Select all

2018-11-22 18:54:29.098 Status: User: Admin initiated a switch command (276/Kitchen radio/Set Level)
2018-11-22 18:54:29.609 Status: dzVents: Info: sshPass: ------ Start external script: kitchen_radio2.lua: Device: "Kitchen radio (Dummy)", Index: 276
2018-11-22 18:54:29.890 Status: dzVents: Debug: kitchen_radio2:
2018-11-22 18:54:29.890 Status: dzVents: Debug: kitchen_radio2: 6
2018-11-22 18:54:29.890 Status: dzVents: Info: kitchen_radio2: ------ Finished kitchen_radio2.lua 
Could it be the same as this or related to this Both pointing to something with Host public key is unknown
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MikeF
Posts: 350
Joined: Sunday 19 April 2015 0:36
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.2
Location: UK
Contact:

Re: dzVents os.execute not executing command [Solved]

Post by MikeF »

Thanks for these pointers.

The first doesn't work, even after adding the ssh fingerprint of the remote RPi through this command:

Code: Select all

ssh-keyscan -H 192.168.0.9 >> ~/.ssh/known_hosts
However, the second one does! :D - by ignoring the check of the host public key, as follows:

Code: Select all

sshpass -p <password> ssh [email protected] -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null mpc play 1
I still don't understand why the original sshpass command works at the command line, but not in dzVents (or a python or bash script, for that matter).

Thanks for your help! :D :D
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest