problem with os.execute in dzVents, while in "normal" lua all is fine  [Solved]

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

Moderator: leecollings

Post Reply
Maarten112
Posts: 6
Joined: Wednesday 06 January 2016 11:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

problem with os.execute in dzVents, while in "normal" lua all is fine

Post by Maarten112 »

Hi guy's,

I'm having a bit of trouble to do the following using dzVents:

- Shutdown a remote host using os.execute over ssh from a local host (e.g. domoticz that shutsdown another raspbeery pi)

What did I do:
- installing keys to remotely ssh without a password ( :arrow: works)
- verified from local cli on domoticz pi that command used is working to shutdown remote host ( :arrow: works)
- verified from local cli on domoticz pi that using "normal" lua without dzvents, code is working ( :arrow: works)
- Using a script on the remote host to shutdown, containing simple "sudo shutdown now" triggered over ssh from local host with

Code: Select all

ssh [email protected] sh /home/pi/shutdown.sh
( :arrow: works)
- Directly triggering from local host to remote host with

Code: Select all

ssh [email protected] sh sudo shutdown now
(I added the pi users to the visudo list to directly use "shutdown" instead of /sbin/shutdown) ( :arrow: works)
- trying both the "os.execute" and "assert(io.popen(cmd, 'r')" way using a standard lua script on local host ( :arrow: works)

What is the problem:
When using dzvents do accomplish the same as above, it is not working. Even if relevant parts of lua codes that works in standard lua get copied to dzvents.


Full Script:

Code: Select all

return {
	on = {
		devices = { 
		'Werkkamer - Sw_L'
		}
	},

	logging = {
		level = domoticz.LOG_DEBUG,
		marker = "P3STEELSWITCH.LUA"
	},

	execute = function(domoticz, switch)
		local cmd = "ssh [email protected] sh /home/pi/shutdown.sh"

		domoticz.log(switch.state)
		
		if(domoticz.devices('Werkkamer - P3Steel').state == 'On') then
			
			if(domoticz.devices('Werkkamer - P3Steel').lastUpdate.minutesAgo >= 1) then

					domoticz.log("3D printer powered on > 1 minute ago, shutdown")
					domoticz.log(cmd)
					
					os.execute(cmd)
					
					--local f = assert(io.popen(cmd, 'r'))
					--s = assert(f:read('*a'))
					--f:close()
					--print(s)
					--domoticz.log(s)
					
					
					domoticz.devices('Werkkamer - P3Steel').switchOff()
				
				else	
				
					domoticz.log("3Dprinter powered on < 1 minute ago, don't shutdown.")

				end
		
		else
		
			domoticz.devices('Werkkamer - P3Steel').switchOn()

		end
	end
}
The relevants part that must trigger the remote host to shutdown are these:

Code: Select all

local cmd = "ssh [email protected] sh /home/pi/shutdown.sh"

Code: Select all

os.execute(cmd)
or

Code: Select all

local f = assert(io.popen(cmd, 'r'))
s = assert(f:read('*a'))
f:close()
--print(s)
domoticz.log(s)
I hope that somebody can help me out. Thanks already!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: problem with os.execute in dzVents, while in "normal" lua all is fine

Post by waaren »

Maarten112 wrote: Tuesday 14 April 2020 14:31 I'm having a bit of trouble to do the following using dzVents:
- Shutdown a remote host using os.execute over ssh from a local host (e.g. domoticz that shutsdown another raspbeery pi)

I hope that somebody can help me out. Thanks already!
I suspect that it has something to do with the user executing the local ssh command being another one as the one you tested with or use another environment. For some other tasks I use ssh with generated keys (without password) from dzVents (root - root) without problems.

I use below function for my os commands because it gives me more debug information

Code: Select all

        local function osCommand(cmd)
            domoticz.log('Executing Command: ' .. cmd,domoticz.LOG_DEBUG)

            local fileHandle = assert(io.popen(cmd .. ' 2>&1 || echo ::ERROR::', 'r'))
            local commandOutput = assert(fileHandle:read('*a'))
            local returnTable = {fileHandle:close()}

            if commandOutput:find '::ERROR::' then     -- something went wrong
               domoticz.log('Error ==>> ' .. tostring(commandOutput:match('^(.*)%s+::ERROR::') or ' ... but no error message
' ) ,domoticz.LOG_DEBUG)
            else -- all is fine!!
                domoticz.log('ReturnCode: ' .. returnTable[3] .. '\ncommandOutput:\n' .. commandOutput, domoticz.LOG_DEBUG)
            end

            return commandOutput,returnTable[3] -- rc[3] contains returnCode

        end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Maarten112
Posts: 6
Joined: Wednesday 06 January 2016 11:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: problem with os.execute in dzVents, while in "normal" lua all is fine

Post by Maarten112 »

Hi Waaren,

Thanks for your reply! Indeed, it is the only possible explanation I could think of also. However, at the same time I think it's a bit weird.

Let's suppose dzvents is running as user root. As user root, the local system where domoticz is running does " ssh [email protected]". This way, the user ssh'ing to the remote system is pi, despite the fact that dzvents is running as user root.

I am not in the opportunity right now, but I will check with your code if i'm able to debug something. One of the problems was that I didn't know how to get some detaild debug output with DzVents (and in "plain" lua all went fine.....).

Also, I will try what happens when I add user 'root' to the remote system and give the user the right permissions.
Maarten112
Posts: 6
Joined: Wednesday 06 January 2016 11:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: problem with os.execute in dzVents, while in "normal" lua all is fine  [Solved]

Post by Maarten112 »

my o my.....thanks to your little function everything is working now! :mrgreen: I did a good old fuckup...... :oops:

ssh keys where only available to pi user, not root..... Dzvents running as root.....so didn't had any keys to connect to the remote host :oops:
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest