VS Code Terminal Window: How to automatically ssh to openhab server

Dear Community,

Is there a way to automatically establish an ssh connection to the openhab server when clicking on the terminal window in VS Code? At present, I only get shell access to the local computer where I can manually open an ssh connection.

Best regards,
Peter

Whenever I use VS Code with ssh and close the window when I am done it automatically reconnects when I reopen the window.

This is a VS Code usage question outside the topic of this forum.The fact you are accessing OH configuration files is incidental to your issue.

Sure, easy.

Prerequisites:

  1. ssh installed (Windows10 (since ~ 19.10?) already has an installed ssh client).
  2. a key pair without password to login (public key listed in .ssh/authorized_keys)
  3. a configuration in VSCode like this:
"openhab.karafCommand": "ssh.exe  -i C:\\path\\to\\openhab.id_rsa user@server-ip"

Please be aware that the option is meant to directly open a connection to the karaf console. This would be port 8101 and another configuration in question of public key. But you can also use this command to connect to the GNU/Linux bash.

You can call the console via ctrl shift P → openhab: Open openHAB console (will be first command in list after using it or the first time)

Dear Udo,

Thank you very much for your explanations. I had the openhab.karafCommand configured, but without any reference to a public key. Please note that I work on an iMac with macOS Catalina 10.15.7. having slightly different shell commands as compared to Windows, but the command is adapted to match the expected syntax.

I managed to successfully install a key pair so that ssh login from my desktop shell to the openhab server on port 22 is working fine, without being prompted for a password:

ssh openhab@<myOpenHabServerIP>

However, if I modify this to access the Karaf Console, like:

# without explicitly defining the key
ssh openhab@<myOpenHabServerIP> -p 8101

# with specifying the key
ssh -i ~/.ssh/id_rsa openhab@<myOpenHabServerIP> -p 8101

…then, in both cases, I need to enter my password, although ~/.ssh/id_rsa on my local computer holds my key.

I can also login to the openhab server shell (i.e. not the Karaf Console) when I explicitly refer to the key:

ssh -i ~/.ssh/id_rsa openhab@<myOpenHabServerIP>

Therefore, I assumed that when entering exactly the aforementioned command into openhab.karafCommand, at least I should properly login at the openhab server when opening the terminal window in VS Code.

However, it appears that my VS Code does not consider this command at all. The only thing that happens when opening terminal is that the profile of my zsh is executed and I am prompted to enter a command. The openhab.karafCommand is not executed at all.

Best regards,
Peter

Yes, that’s the point I referred to…

For direct access to karaf, the command needs additional parameter -p8101 (as you already know).
But the point is, karaf is not the openssh server from GNU/Linux, but another ssh server, builtin in karaf.

So, you have to add the public key to another file, not .ssh/authorized_keys, but instead in the file $OPENHAB_USERDATA/etc/keys.properties. Be aware that the format is different to authorized_keys
There is an example in the file:

#karaf=AAAAB3NzaC1kc3MAAACBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9EAMMeP4C2USZpRV1AIlH7WT2NWPq/xfW6MPbLm1Vs14E7gB00b/JmYLdrmVClpJ+f6AR7ECLCT7up1/63xhv4O1fnxqimFQ8E+4P208UewwI1VBNaFpEy9nXzrith1yrv8iIDGZ3RSAHHAAAAFQCXYFCPFSMLzLKSuYKi64QL8Fgc9QAAAIEA9+GghdabPd7LvKtcNrhXuXmUr7v6OuqC+VdMCz0HgmdRWVeOutRZT+ZxBxCBgLRJFnEj6EwoFhO3zwkyjMim4TwWeotUfI0o4KOuHiuzpnWRbqN/C/ohNWLx+2J6ASQ7zKTxvqhRkImog9/hWuWfBpKLZl6Ae1UlZAFMO/7PSSoAAACBAKKSU2PFl/qOLxIwmBZPPIcJshVe7bVUpFvyl3BbJDow8rXfskl8wO63OzP/qLmcJM0+JbcRU/53JjTuyk31drV2qxhIOsLDC9dGCWj47Y7TyhPdXh/0dthTRBy6bqGtRPxGa7gJov1xm/UuYYXPIUR/3x9MAZvZ5xvE0kYXO+rx,_g_:admingroup

_g_\:admingroup = group,admin,manager,viewer,systembundles

This is the username (would be openhab in openHAB2, or the username you chose when first starting openHAB3), followed by =, the key (between = and , ) and the correct group (should be _g_:admingroup)

The user is commented out as it’s an example :slight_smile: so don’t write the # in front of the username…

Dear Udo,

Your comments helped me to access the Karaf Console without entering a password from the zsh shell of my desktop computer. Thank you very much. But the behavior inside VS Code remains unchanged, even after restarting both OpenHAB and VS Code.

I have entered the following configuration settings (of course with the real IP of the openhab server) in VS Code:

"openhab.karafCommand": "ssh -i ~/.ssh/id_rsa openhab@<OpenHabServerIP>",

Besides, I have also tried:

"openhab.karafCommand": "ssh -i ~/.ssh/id_rsa openhab@<OpenHabServerIP> -p 8101",

However, when switching to the terminal window in VS Code, I still receive a new line in the shell of my local desktop. There is no ssh command executed. Maybe I invoke the terminal functionality in a wrong way. I simply click on “Terminal” in the menu line where “Problems”, Output", “Debug Console” are listed right besides “Terminal”.

Best regards,
Peter

Ah. No, you have to use the command:

ctrlshiftP, then type openHAB: Open openHAB console (you only have to type few letters and then click at the command)

Additionally, you can link a Shortcut to the command (I’m using ctrlshiftaltK)

Is there a reason you can’t/don’t want to use the remote ssh extension? That will give you the remote server’s console instead of the local.

Dear Udo and Anders,

Thank you both for your hints. I am now able to automatically open die Karaf Console via the OpenHAB extension (which does not work with Remote SSH because of the very specific SSH server of the Karaf Console) and Remote SSH allows me to get access to the OpenHAB server.

Best regards,
Peter

Just for information:

You can also add keybindings to the conseole commands if you don’t want to use cmdshiftp all the time. :slight_smile:

offtopic: how to create keys instead of text? (e.g. <shift> )?

You can do that with the keyboard <kbd>BLABLUB</kbd> html tag. :slight_smile:

<kbd>cmd</kbd><kbd>shift</kbd><kbd>p</kbd>

will result in cmdshiftp

1 Like

Ah, cool! Thanks.

1 Like

Hello

is there a way to get this working with docker?
it kinda bugs me that i have to ssh into my synology first and then use

docker exec -it openhab /openhab/runtime/bin/client

the rest api is working in vscode. the terminal would be great to use openhab:send
or is there another way of making changes to items? i can see the current values when hovering over them.

regards

jonas

Well, there are some ways to do it…
MY preferred method:

  1. create a public/private key pair (ssh-keygen ...) You may or may not set a password for the key.
  2. copy the content of the public key into the file openhab_userdata/etc/keys.properties and add a reasonable username and group (there is an example in the file)
  3. deactivate the password login for Karaf user openhab (in openhab_userdata/etc/users.properties delete or comment out the user openhab)
  4. open the karaf console for remote access via openhab_userdata/etc/org.apache.karaf.shell.cfg (set sshHost to 0.0.0.0)
  5. add port 8101 to open container ports.
  6. use ssh <username>@<docker-server> -i <path to private key> -p8101 to connect directly to the console.
1 Like

Udo, as always thanks for your great reply. I really appreciate it!
I will try it tomorrow.
I wish you happy holidays!

Thank you so much it works!

Are the changes to the properties files persistent or do i have to change them again after an update?

The files belong to userdata, so it should be persistent.
But please always keep in mind, that sometimes even userdata files can be changed via upgrade, apt should pause in that occasion and ask wether the file should be altered to the new one or stay as the old one.
If the file is changed, the old version will be renamed to *.dpkg-old.
if the file is not changed, the new file will be stored as *.dpkg-dist, so nothing is lost.

I’m not sure about docker, as the software won’t be “upgraded” but the image is changed to a newer one, in this case the configuration should stay as is, because openHAB will only check if a file does not exist, not, if it’s “too old”.

1 Like

Hello

it broke a while ago unfortunately and in addition to that i could not connect from my mac either.
I checked the files and recognized the dns was wrong so i reset it to 0.0.0.0.
The rest of the config was right and i can connect from my mac termianl using this: ssh jonas@192.168.1.20 -i /Volumes/docker/openhab/id_rsa -p8101 again.

set the same in the vscode but it just connects to my synology nas (where docker is installed) and not directly to the console - see images.

Do you have any idea what’s going wrong?

Bildschirm­foto 2023-02-17 um 15.35.58

Nothing with DNS, I hope that only was a typo… sshHost has to be set to 0.0.0.0, this is: Karaf will listen on every Ethernet Port, regardless which IP is set.

I don’t see your console issue (the picture is part of the settings.json.