# 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…

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



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

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

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

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


will result in cmdshiftp

1 Like

Ah, cool! Thanks.

1 Like