Page 1 of 1
ssh
Posted: Saturday 11 November 2023 9:32
by jacobsentertainment
Hi all,
Is there a way I can run a command in ssh from dzvent?
I'm working on a script to trigger motioneye to start recording based on my motion detection. If I perform the commends from my domoticz pi ssh terminal it works fine, but as soon as I try to run it from domoticz nothing will happen.....
Code: Select all
return {
on = {
devices = {
669,
}
},
logging = {
level = domoticz.LOG_DEBUG,
marker = "motioneye"
},
execute = function(dz, device)
local motionEyeIP = "192.1.10.222"
local motionEyeUsername = "pi"
local motionEyeCommand = 'curl "http://localhost:7999/1/config/set?emulate_motion=1"'
local sshCommand = 'ssh ' .. motionEyeUsername .. '@' .. motionEyeIP .. " '" .. motionEyeCommand .. "'"
dz.log('SSH-commando: ' .. sshCommand, dz.LOG_INFO)
os.execute(sshCommand)
dz.log('SSH-commando op MotionEye', dz.LOG_INFO)
end
}
Re: ssh
Posted: Saturday 11 November 2023 10:14
by Kedi
What is the exact output in the log file of
Code: Select all
dz.log('SSH-commando: ' .. sshCommand, dz.LOG_INFO)
And does that command execute correct in a 'bash' terminal?
There might be a mismatch on the ' and " characters.
Re: ssh
Posted: Saturday 11 November 2023 10:18
by Kedi
You might also add to the ssh command
Because on the prompt you run ssh as user pi and from Domoticz as user root.
You could also put the complete ssh command string in a bash script and run that script from dzVents
Don't forget to put #!/bin/bash on the first line in that script.
Re: ssh
Posted: Saturday 11 November 2023 10:20
by jacobsentertainment
I was looking in to this as wel but tried several thiings, nothing seems to have a positive result.
Instead of running the complete command I test it with simply creating a new folder with "mkdir b" and the log feedback is,
Code: Select all
2023-11-11 10:17:49.674 Status: dzVents: Info: motioneye: Uit te voeren SSH-commando: ssh mkdir b
Re: ssh
Posted: Saturday 11 November 2023 10:24
by jacobsentertainment
Sorry this was a local test on the machine with domoticz, but on the external machine it's the same. no action trough ssh.
I would nearly think there is no communication from the dz script in to the terminal...
Re: ssh
Posted: Saturday 11 November 2023 10:28
by Kedi
In the last command the mkdir b should be 'mkdir b' otherwise you give 2 parameters to ssh
Re: ssh
Posted: Saturday 11 November 2023 10:50
by jacobsentertainment
Tried that to, no change to the the outcome
Also tried on my local machine
- ssh mkdir c
- ssh mkdir /home/pi/ c
nothing seems to have a outcome that works. I'm not that good with all this scritpting, most is copy/paste and if it works continue or ctrl-z

Re: ssh
Posted: Saturday 11 November 2023 10:56
by Kedi
Put your complete command the a bash script like
Make that script executable and run that script as pi and again as root e.g. sudo <scriptname>
If that works it should also work if you call your bash script from dzVents.
Don't forget the " -o StrictHostKeyChecking=no " option added to the ssh command.
Re: ssh
Posted: Saturday 11 November 2023 11:04
by jacobsentertainment
Kedi wrote: ↑Saturday 11 November 2023 10:56
Put your complete command the a bash script like
Make that script executable and run that script as pi and again like root e.g. sudo <scriptname>
If that works it should also work if you call your bash script from dzVents.
Don't forget the " -o StrictHostKeyChecking=no " option added to the ssh command.
Sorry but this is just a small step outside my knowledge

how do i apply this to current scritp? (sorry to ask)
Code: Select all
return {
on = {
devices = {
669,
}
},
logging = {
level = domoticz.LOG_DEBUG,
marker = "motioneye"
},
execute = function(dz, device)
local motionEyeIP = "192.1.10.222"
local motionEyeUsername = "pi"
local motionEyeCommand = 'curl "http://localhost:7999/1/config/set?emulate_motion=1"'
local sshCommand = 'ssh ' .. motionEyeUsername .. '@' .. motionEyeIP .. " '" .. motionEyeCommand .. "'"
dz.log('SSH-commando: ' .. sshCommand, dz.LOG_INFO)
os.execute(sshCommand)
dz.log('SSH-commando op MotionEye', dz.LOG_INFO)
end
}
Re: ssh
Posted: Saturday 11 November 2023 11:49
by Kedi
Change
Code: Select all
local sshCommand = 'ssh ' .. motionEyeUsername .. '@' .. motionEyeIP .. " '" .. motionEyeCommand .. "'"
in
Code: Select all
local sshCommand = '<scriptpath and scriptname> '
Re: ssh
Posted: Saturday 11 November 2023 12:13
by Kedi
Did you try dz.executeShellCommand instead of the os.execute command?
Re: ssh
Posted: Saturday 11 November 2023 20:13
by jacobsentertainment
Kedi wrote: ↑Saturday 11 November 2023 12:13
Did you try dz.executeShellCommand instead of the os.execute command?
Tried many options by now,
Code: Select all
return {
on = {
devices = {
669,
}
},
logging = {
level = domoticz.LOG_DEBUG,
marker = "ssh"
},
execute = function(domoticz, device)
local sshCommand = 'ssh [email protected] http://localhost:7999/1/config/set?emulate_motion=1 '
domoticz.log('Uit te voeren SSH-commando: ' .. sshCommand, domoticz.LOG_INFO)
domoticz.executeShellCommand(sshCommand)
end
}
Currently testing locally first to see if I can create a new directory on my local machine, but not even that works out.
Code: Select all
return {
on = {
devices = {
669,
}
},
logging = {
level = domoticz.LOG_DEBUG,
marker = "ssh"
},
execute = function(domoticz, device)
local sshCommand = 'ssh mkdir /home/pi/ new '
domoticz.log('Uit te voeren SSH-commando: ' .. sshCommand, domoticz.LOG_INFO)
--os.execute(sshCommand)
domoticz.executeShellCommand(sshCommand)
end
}
So I'm still trying to figure out the "root" and "pi" permissions.
Re: ssh
Posted: Sunday 12 November 2023 8:39
by Kedi
I am confused. Are 192.1.10.222 and localhost the same machine? And this is other machine as where dzVents script is running? Right?
If so, then it is all about putting the right ' and " on the right places.
Better solution might be a local bash script on the remote machine that does the job and let ssh call that script.
Re: ssh
Posted: Sunday 12 November 2023 11:51
by jacobsentertainment
I have domoticz running on 192.1.10.72 and motioneye o runs on 192.1.10.222
Communication trough ssh works fine and with putty i run
the command is executed as intended and the camera starts recording. But from dzvent scritp nothing happens.
Log data:
Code: Select all
2023-11-12 11:41:30.076 Status: User: Olaf (IP: 192.1.10.114) initiated a switch command (669/test ssh/On)
2023-11-12 11:42:13.066 Status: dzVents: Info: Handling events for: "test ssh", value: "On"
2023-11-12 11:42:13.066 Status: dzVents: Info: ssh: ------ Start internal script: ssh test5: Device: "test ssh (Dummy)", Index: 669
2023-11-12 11:42:13.066 Status: dzVents: Debug: ssh: ExecuteShellCommand: command = sshCommand
2023-11-12 11:42:13.066 Status: dzVents: Debug: ssh: ExecuteShellCommand: callback = mycallbackstring
2023-11-12 11:42:13.066 Status: dzVents: Debug: ssh: ExecuteShellCommand: timeout = 10
2023-11-12 11:42:13.066 Status: dzVents: Debug: ssh: ExecuteShellcommand: path = /home/pi/domoticz/scripts/dzVents/data/
2023-11-12 11:42:13.066 Status: dzVents: Info: ssh: Uitgevoerd commando: ssh [email protected] curl "http://localhost:7999/1/config/set?emulate_motion=1"
2023-11-12 11:42:13.066 Status: dzVents: Info: ssh: ------ Finished ssh test
In the log the command that's send to ssh looks like it should be to me.
192.1.10.114 = my laptop 
Re: ssh
Posted: Sunday 12 November 2023 11:58
by Kedi
Did you try directly?
Code: Select all
curl -X POST http://192.11.10.222:7999/1/config/set?emulate_motion=1
And if you want to go true ssh
Change the position of the first "
Re: ssh
Posted: Sunday 12 November 2023 13:53
by jacobsentertainment
Kedi wrote: ↑Sunday 12 November 2023 11:58
Did you try directly?
Code: Select all
curl -X POST http://192.1.10.222:7999/1/config/set?emulate_motion=1
And if you want to go true ssh
Change the position of the first "
Tried both, best result so far is with
Code: Select all
curl -X POST http://192.11.10.222:7999/1/config/set?emulate_motion=1
as I'm getting at least a positive login to the other machine
Code: Select all
Nov 12 13:45:01 raspberry CRON[7487]: pam_unix(cron:session): session opened for user root(uid=0) by (uid=0)
Nov 12 13:45:01 raspberry CRON[7487]: pam_unix(cron:session): session closed for user root
But the command to motioneye is not executed yet. so still testing and playing around
Re: ssh
Posted: Sunday 12 November 2023 16:08
by jacobsentertainment
After some more testing I added the absolute path's to ssh, created an private key and also added the full path to the auth key.
So working script:
Code: Select all
return {
on = {
devices = {669}, -- Adjust the device ID accordingly
},
logging = {
level = domoticz.LOG_DEBUG,
marker = "ssh",
},
execute = function(domoticz, device)
local privateKeyPath = '/home/pi/.ssh/id_rsa'
local remoteCommand = 'curl "http://localhost:7999/1/config/set?emulate_motion=0"'
local sshCommand = '/usr/bin/ssh -i ' .. privateKeyPath .. ' [email protected] "' .. remoteCommand .. '" '
domoticz.log('Executing SSH command: ' .. sshCommand, domoticz.LOG_INFO)
local success, exitCode, result = os.execute(sshCommand)
domoticz.log('SSH Command Result: ' .. tostring(result), domoticz.LOG_INFO)
domoticz.log('SSH Command Exit Code: ' .. tostring(exitCode), domoticz.LOG_INFO)
domoticz.log('Executed SSH-command: ' .. sshCommand, domoticz.LOG_INFO)
end
}