Command_line :action not on the local host

I want to issue a command on another computer on the same LAN
the commands are, and need to be issued as root,:
systemctl stop
systemctl start

I can set up password less ssh login , and I think I can login as root with SSH
so maybe something like:-

ssh [email protected]>ip_address> && systemctl stop <service>

words of wisdom please

Should be fine, although you don’t want the “&&” - SSH takes an optional command as a parameter, so just:

ssh [email protected] systemctl stop <service>

It might be a good idea to set up a separate account for it rather than using root though.

Thanks Micheal
The only thing running on the old OdroidC1 is motion, and as a minimal install the default user is root,
which is convenient, as systemctl need to been executed as root . What would be nice is to get some feedback the action has been completed, I did think of the process id, but it returns

[email protected]:/etc/motion$ ssh [email protected] "ps ax|grep motion"
26676 pts/0    S+     0:00 tail -f /var/log/motion/motion.log
26857 ?        Ss     0:00 bash -c ps ax|grep motion
26859 ?        S      0:00 grep motion

forget the log line as I had that running to see if it worked, in an automation it doesn’t matter about feedback, but would be nice to have a switch in lovelace as well

Another problem is it works from the CLI of the host computer, but not from HA

  - platform: command_line
#        command_on: 'lwp-request http://localhost:7770/103/detection/start'
        command_on: 'ssh [email protected] "systemctl restart motion"'
        command_off: 'ssh [email protected] "systemctl stop motion"'

the hashed line worked but was very hit and miss, I wondered if it was not reading /etc/hosts on the host computer, hence the full ip_address, but made no difference, maybe execute a shell script on the host ???

I added the full ssh path /usr/bin/ssh but it still didn’t work
so I put

ssh [email protected] "systemctl restart motion

in a shell script on the host and called that and that works.

it would be nice if I put in the shell script, there are two one for on and one for off

ssh [email protected] systemctl restart motion
sleep 3
ssh [email protected] systemctl status motion

then test for the status , maybe on the “Active” line , but not sure how to do this

● motion.service - Motion detection video capture daemon
     Loaded: loaded (/etc/systemd/system/motion.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2022-11-29 13:34:33 UTC; 22min ago
       Docs: man:motion(1)
   Main PID: 29256 (motion)
     Memory: 54.3M
     CGroup: /system.slice/motion.service
             └─29256 /usr/bin/motion -c /etc/motion/motion.conf -d 8

Any help appreciated

I do believe you have to put your ssh keys in your config folder under a folder called ssh_keys that you create, and then you need to reference like this:

  - platform: command_line
        command_on: 'ssh -i /config/ssh_keys/yourkeyname -o StrictHostKeyChecking=no [email protected] systemctl restart motion'
        command_off: 'ssh -i /config/ssh_keys/yourkeyname -o StrictHostKeyChecking=no [email protected] systemctl stop motion'

I think that will get you going.

Thanks TinyDot
I already have the ssh keys installed as from the CLI on the host I can

ssh [email protected]  systemctl restart motion

That logs into the remote machine without asking for a password
keyname is that the name of the user that created the key, in my case on the host machine that is odroid.
I’m also using ssh on the binary_sensor to check the status, I’ll try on that , Thanks

The ssh keys have to be in the Homeassistant container, and referenced to run from there, cause it is a different environment, than the host. Also the keyname, is the name of the file that your key is called i.e. id_rsa, or what ever you named it.

OK, I’m running core, so to generate the keys , do I need to cd to /srv/homeassistant, activate, then generate the key with

ssh-keygen -t rsa -b 4096

then where does that key go to stay within the homeassistant environment, which the user is also odroid.
thanks for the help

It sounds like you already have the keys, made cause you said this works from the host, so just create a folder called ssh_keys in you Homeassistant config folder, and place the key in that folder. Then you should be all set using the switch config I gave you above.

1 Like

thanks, config folder is that /home/odroid/.homeassistant or /srv/homeassistant

I am not sure how you have setup your install, but your config folder will have your configuration.yaml, automation.yaml etc file in it.

I’ve added both SSH keys into /srv/homeassistant and .homeassistant

  - platform: command_line
    name: Cam_status
    command: 'ssh -i /config/ssh_keys/id_rsa -o StrictHostKeyChecking=no [email protected] "systemctl is-active motion"'
    payload_on: "active"
    payload_off: "inactive"

Doing something as before the change in States it was “on” all the time , now its “unknown”

But it did change to “on” when the cams were enabled , but when stopped it did not change state.
nearly working , Thanks

I’m glad you are closer to the end, and I hope you figure out the rest.

hopefully this question will get it working 100%
When HA is running , where will it look for the ssh_key directory as it running in its own environment.
the path to the ssh_key directory on the host machine is /home/odroid/.homeassistant ???
if HA’s root directory is .homeassistant , then should it be

'ssh -i ./ssh_keys/id_rsa -o StrictHostKeyChecking=no [email protected] "systemctl is-active motion"'

I’m So Sorry I missed this until now : ( You create a directory in you HomeAssistant config folder ( The folder that contains your automation.yaml, configuration.yaml and so ) Then you would point to you keys in that config folder:

echo `ssh -i /config/ssh/id_rsa

My keys are in the HA config folder in the ssh folder. Sorry for the tardiness.