Exec ssh command save password?

Hi, i am trying to turn the display of a second raspberry on and off via a ssh command.
As soon as it is working in the console i want to create an exec command for it.

However the console is always asking for a password which i have to insert manually:

[13:59:38] openhabian@openhab:~$ ssh pi@192.168.0.199 sudo reboot
The authenticity of host ‘192.168.0.199 (192.168.0.199)’ can’t be established.
ECDSA key fingerprint is SH####:lPO2##################.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.0.199’ (ECDSA) to the list of known hosts.
pi@192.168.0.199’s password:
Connection to 192.168.0.199 closed by remote host.

After this was done once i can now power on and off the display via:

[14:03:38] openhabian@openhab:~$ ssh pi@192.168.0.199 sudo vcgencmd display_power 1
pi@192.168.0.199’s password:
display_power=1

Can i insert the password in the command or save it somewhere?
Thank you!

Use key authorization and set no password on the key. Google is your friend here.

2 Likes

Usually we don’t use passwords in “automations” like this. Generate rsa keys and use that instead, so you don’t need to type any password at all.

2 Likes

I will echo what the others said. Study and learn how to use ssh features.

1 Like

Thank you for the hints.
I googled some information and did this:

[15:36:37] openhabian@openhab:~$ sudo -u openhab ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/openhab2/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/openhab2/.ssh/id_rsa.
Your public key has been saved in /var/lib/openhab2/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:BEuJD"######" openhab@openhab
The key’s randomart image is:
±–[RSA 2048]----+
| “######” . .E=|
| o.“####”+o . =|
| o *X"###" oo|
| o. . .|
| S |
| |
| |
| |
| |
±—[SHA256]-----+

[15:40:39] openhabian@openhab:~$ sudo -u openhab ssh-copy-id -i /var/lib/openhab2/.ssh/id_rsa.pub pi@192.168.0.199
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/var/lib/openhab2/.ssh/id_rsa.pub”
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys

pi@192.168.0.199’s password:

Number of key(s) added: 1

Now try logging into the machine, with: “ssh ‘pi@192.168.0.199’”
and check to make sure that only the key(s) you wanted were added.

[15:43:51] openhabian@openhab:~$ sudo ssh -i /var/lib/openhab2/.ssh/id_rsa.pub pi@192.168.0.199 sudo vcgencmd display_power 1The authenticity of host ‘192.168.0.199 (192.168.0.199)’ can’t be established.
ECDSA key fingerprint is SHA256:lPO2"###########".
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.0.199’ (ECDSA) to the list of known hosts.
Load key “/var/lib/openhab2/.ssh/id_rsa.pub”: invalid format
pi@192.168.0.199’s password:
display_power=1

[15:44:22] openhabian@openhab:~$ sudo -u openhab ssh -i /var/lib/openhab2/.ssh/id_rsa.pub pi@192.168.0.199 sudo vcgencmd display_power 1
Load key “/var/lib/openhab2/.ssh/id_rsa.pub”: invalid format
pi@192.168.0.199’s password:
display_power=1

I replaced some values in the fingerprints etc.

I am stuck now. Why is it “invalid format”?

i think i found the problem. The *.pub has to be replaced. So the correct command seems to be:

[15:44:22] openhabian@openhab:~$ sudo -u openhab ssh -i /var/lib/openhab2/.ssh/id_rsa pi@192.168.0.199 sudo vcgencmd display_power 1

1 Like