ssh

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

Moderator: leecollings

Post Reply
User avatar
jacobsentertainment
Posts: 211
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

ssh

Post 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
}
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: ssh

Post 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.
Logic will get you from A to B. Imagination will take you everywhere.
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: ssh

Post by Kedi »

You might also add to the ssh command

Code: Select all

-o StrictHostKeyChecking=no
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.
Last edited by Kedi on Saturday 11 November 2023 10:21, edited 1 time in total.
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
jacobsentertainment
Posts: 211
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: ssh

Post 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
User avatar
jacobsentertainment
Posts: 211
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: ssh

Post 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.

Code: Select all

2023-11-11 10:22:44.521 Status: dzVents: Info: motioneye: Uit te voeren SSH-commando: ssh [email protected] mkdir b
I would nearly think there is no communication from the dz script in to the terminal...
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: ssh

Post by Kedi »

In the last command the mkdir b should be 'mkdir b' otherwise you give 2 parameters to ssh
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
jacobsentertainment
Posts: 211
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: ssh

Post by jacobsentertainment »

Tried that to, no change to the the outcome

Code: Select all

2023-11-11 10:35:21.235 Status: dzVents: Info: motioneye: Uit te voeren SSH-commando: ssh [email protected] 'mkdir c'
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 ;)
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: ssh

Post by Kedi »

Put your complete command the a bash script like

Code: Select all

#!/bin/bash

ssh [email protected] "mkdir b"
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.
Last edited by Kedi on Sunday 12 November 2023 8:41, edited 2 times in total.
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
jacobsentertainment
Posts: 211
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: ssh

Post by jacobsentertainment »

Kedi wrote: Saturday 11 November 2023 10:56 Put your complete command the a bash script like

Code: Select all

#!/bin/bash

ssh [email protected] "mkdir b'
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 :oops: 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
}
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: ssh

Post by Kedi »

Change

Code: Select all

        local sshCommand = 'ssh ' .. motionEyeUsername .. '@' .. motionEyeIP .. " '" .. motionEyeCommand .. "'" 
in

Code: Select all

        local sshCommand = '<scriptpath and scriptname> ' 
Logic will get you from A to B. Imagination will take you everywhere.
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: ssh

Post by Kedi »

Did you try dz.executeShellCommand instead of the os.execute command?
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
jacobsentertainment
Posts: 211
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: ssh

Post 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.
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: ssh

Post 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.
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
jacobsentertainment
Posts: 211
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: ssh

Post 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

Code: Select all

ssh [email protected] curl "http://localhost:7999/1/config/set?emulate_motion=1"
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 ;-)
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: ssh

Post 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

Code: Select all

ssh [email protected] "curl http://localhost:7999/1/config/set?emulate_motion=1"
Change the position of the first "
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
jacobsentertainment
Posts: 211
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: ssh

Post 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

Code: Select all

ssh [email protected] "curl http://localhost:7999/1/config/set?emulate_motion=1"
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
User avatar
jacobsentertainment
Posts: 211
Joined: Thursday 01 October 2020 1:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021-1
Location: Not @ home
Contact:

Re: ssh

Post 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
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest