Change logging level through Basic UI and access Karaf in a rule with OH 3.4.2

Hi

i was following this tutorial Change logging level through a UI and access Karaf in a rule
to change the log level through the basic ui with a rule which access the karaf console.

i use openhabian 3.4.2 on raspberry pi and already changed the (triggeringItem.getName) in the rule with triggeringItemName.
that works fine but i was not able to run the ssh command (yes - it works if i try it in a shell).

to test a little bit faster i modified the execute command and tried one by one:

//executeCommandLine("/bin/bash@@-c@@/usr/bin/ssh openhab@localhost -p 8101 log:set INFO openhab.event.ItemStateChangedEvent")
//executeCommandLine("/bin/bash@@-c@@/usr/bin/ssh", "openhab@localhost", "-p", "8101", "log:set", "INFO", " openhab.event.ItemStateChangedEvent")
//executeCommandLine("/bin/bash@@-c@@/usr/bin/ssh", "openhab@localhost", "-p", "8101", "log:set", "INFO", "", "openhab.event.ItemStateChangedEvent")
executeCommandLine("/bin/bash", "-c", "/usr/bin/ssh", "openhab@localhost", "-p", "8101", "log:set", "INFO", "", "openhab.event.ItemStateChangedEvent")

i always got the nearly same error:
2023-04-11 21:32:57.354 [WARN ] [rg.openhab.core.io.net.exec.ExecUtil] - Error occurred when executing commandLine ‘[/bin/bash@@-c@@/usr/bin/ssh, openhab@localhost, -p, 8101, log:set, INFO, openhab.event.ItemStateChangedEvent]’
java.io.IOException: Cannot run program “/bin/bash@@-c@@/usr/bin/ssh”: error=2, No such file or directory

  • at java.lang.ProcessBuilder.start(ProcessBuilder.java:1128) ~[?:?]*
  • at java.lang.ProcessBuilder.start(ProcessBuilder.java:1071) ~[?:?]*
  • at org.openhab.core.io.net.exec.ExecUtil.executeCommandLine(ExecUtil.java:59) ~[?:?]*
  • at org.openhab.core.model.script.actions.Exec.executeCommandLine(Exec.java:40) ~[?:?]*

only the last one work - but it did not update the log level.

any ideas ? thanks a lot!

Andi

The way to use executeCommandLine has changed. Formerly @@ was used to fake a space within a command.

You don’t need the bash (pretty sure of that part)
So the correct command should be

executeCommandLine("/usr/bin/ssh", "localhost", "-p8101", "log:set", "INFO", "", "openhab.event.ItemStateChangedEvent")

You don’t need a space between -p and 8101 and as openHAB will use openhab User, you don’t need to set this either :slight_smile:

BUT.

This won’t work as intended, because karaf user openhab is secured with a password. :slight_smile: There is no way to set the password via option (like the port).
The correct way to circumvent this situation is to use a key file.
See the link within the thread you already linked to:

If set correctly, you don’t need to use the -i option.
There might be a pitfall (had this in my test system and it gave me some headache), and this is, that some permissions might be not set correct.

openHABian 1.7.5 (see Releases · openhab/openhabian · GitHub) sets the home directory for User openhab (which runs openHAB) to /var/lib/openhab.
You have to create a directory /var/lib/openhab/.ssh for the keys to let ssh use them without the need of option -i BUT

  • /var/lib/openhab/ must be set to drwxr-xr-x (but is set to drwxrwxr-x)
  • /var/lib/openhab/.ssh must be set to drwx------

The first point is mandatory if you want to use the key to login to the openHAB Server (GNU/Linux shell) and you won’t get any information other than “doesn’t work” unless you don’t look deeper into the logs… I’m not 100% sure if you need that part for using the private key
So ensure that all permissions are set correct:

sudo chmod 755 /var/lib/openhab #maybe you don't need that part
sudo mkdir /var/lib/openhab/.ssh
sudo chown openhab: /var/lib/openhab/.ssh
sudo chmod 700 /var/lib/openhab/.ssh

Now create a key file for User openhab:

sudo -u openhab ssh-keygen -t rsa -f openhab.id_rsa

Make sure NOT to set any password.
Take a look at the newly created directory:

sudo ls -l /var/lib/openhab/.ssh

There should be two files, openhab.id_rsa and openhab.id_rsa.pub
get the content of the pub-file:

sudo cat /var/lib/openhab/.ssh/openhab.id_rsa.pub 

Copy it to your clipboard and put it into the correct file :slight_smile:

sudo nano /var/lib/openhab/etc/keys.properties

Make sure you get a line like that:

openhab=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 example within the file.
Obviously you have to change the part between openhab= and ,_g_:admingroup
Make sure that there is no comment # at the beginning of the line.
Save the file and check wether openHAB now is able to login with Karaf user openhab without using a password:

sudo -u openhab /usr/bin/ssh localhost -p8101

You should get into the karaf console:


                           _   _     _     ____
   ___   ___   ___   ___  | | | |   / \   | __ )
  / _ \ / _ \ / _ \ / _ \ | |_| |  / _ \  |  _ \
 | (_) | (_) |  __/| | | ||  _  | / ___ \ | |_) )
  \___/|  __/ \___/|_| |_||_| |_|/_/   \_\|____/
       |_|       3.4.2 - Release Build

Use '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.
To exit, use '<ctrl-d>' or 'logout'.

Sounds veeery complex, but it’s really simple if you:ve done it more than once :wink:

2 Likes

You also don’t need to use the command line anymore if you’d rather avoid that. OH3 now has the logging available via the rest API. Here’s an example of using the http binding to set up an Item which sets the zwave logging level:

1 Like

@JustinG great to know. i’m an openhab user since 2015 but still very old school :slight_smile:
so i would prefer the solution with the rule, but i will have a look at this way.
i didn’t use api tokens before, but i just checked - it’s very well documented.

@Udo_Hartmann Thank you for your reply and the explanation.
The passwordless login is already working, that was not the problem.
I changed your syntax a little bit, because the shell user ist openhabian and karaf uses the user openhab, so i’ve added the user to the localhost again.

executeCommandLine("/usr/bin/ssh", "openhab@localhost", "-p8101", "log:set", "INFO", "", "openhab.event.ItemStateChangedEvent")

in the shell the command “/usr/bin/ssh openhab@localhost -p8101 log:set INFO openhab.event.ItemStateChangedEvent” works very well,
in the ui i got an exception:

[WARN ] [shd.server.session.ServerSessionImpl] - exceptionCaught(ServerSessionImpl[null@/0:0:0:0:0:0:0:1:32856])[state=Opened] IOException: Broken pipe

funny - i got the same exception during testing with a shellscript, to avoid using spaces with executeCommandLine :slight_smile:

and now to the strange part about the ssh keys. my .ssh directory is in the openhabian home folder /home/.ssh and not in the /var/lib/openhab/.ssh.
should i change it - even if the passwordless login works fine in the shell?

openhabian@knx2:/var/lib/openhab $ /usr/bin/ssh openhab@localhost -p8101 log:get openhab.event.ItemStateChangedEvent
WARN
openhabian@knx2:/var/lib/openhab $ /usr/bin/ssh openhab@localhost -p8101 log:set INFO openhab.event.ItemStateChangedEvent
openhabian@knx2:/var/lib/openhab $ /usr/bin/ssh openhab@localhost -p8101 log:get openhab.event.ItemStateChangedEvent
INFO
openhabian@knx2:/var/lib/openhab $

maybe any ideas what goes wrong?

NO! The shell user isn’t openhabian, but openhab.

The user openhabian is for administration purpose, the GNU/Linux user openhab is used to run openHAB, the (independent) karaf user openhab is used to get into the karaf shell.

Take a look at sudo ps aux | grep /usr/bin/java, you will see that user openhab is running the java process for openHAB, therefor all actions from openHAB are in user context openhab, not openhabian.

The shell variant is started by the user that the shell owns / you are logged in with.
When it is being executed by OH it runs with the OH user privileges.
When an ssh session is executed the first time you are asked to verify and accept the host key if it is not already in the known hosts file.
This verification process may keep a prompt open and thus the command may be aborted.
You either need to add the hosts fingerprint to the known hosts file or you can add a command line option to the ssh command to ignore the fingerprint verification.

OH 4 adds that to the UI now too. There’s a little gear icon on the binding’s page where you can set the logging level.

1 Like

That’s a really nice feature!

@Udo_Hartmann well, sorry for that. you explained everything very good and i jumped over the important parts.
you are absolut right, i mixed up the users openhab and openhabian.

i fixed these things now according your explanations and it works through the UI, but only with the -i parameter.
so i did all twice, but with the same result.
i think the problem is during the key generation - the keys where generated in the current directory and not automatical in the /var/lib/openhab/.ssh folder, i had to copy them in that directory.

rights/owner should be ok:

drwx------ 2 openhab openhab 4096 Apr 13 14:53 .
drwxr-xr-x 14 openhab openhab 4096 Apr 13 14:53 …
-rw-r–r-- 1 openhab openhab 442 Apr 13 14:36 known_hosts
-rw------- 1 openhab openhab 2602 Apr 13 14:53 openhab.id_rsa
-rw-r–r-- 1 openhab openhab 566 Apr 13 14:53 openhab.id_rsa.pub

My bad, didn’t check twice… the filename has to be id_rsa instead of openhab.id_rsa to be used as default key.

yes - know it works without -i

thank you!