Auto-login to karaf console and execute commands via bash script

What I’m trying to do is to login to karaf console via bash script and execute there some commands.
I’m trying to do it like this:

#!/bin/bash
cmd='sudo ssh -p 8101 openhab@localhost'
eval $cmd
cmd='echo "password" && echo log:clear && echo logout'
eval $cmd

How can you suspect, it doesn’t work and it’s freezing at "Password: ", waiting for user to type. Then, when I type the password, karaf is opening and when I close it, the rest of script is executing.
Is there a way to type a password automaticly and execute commands inside?

Yes it can, probably easier then you think:
openhab-cli console -p PASSWORD CMD

ex:
openhab-cli console -p habopen bundle:list

your script can be:

#!/bin/bash
openhab-cli console -p habopen $@
1 Like

Thanks for answer! This is working fine, however it seems now that log:clear command is not working. I remember that I was using it in the past and it worked fine. Any ideas why now nothing is happening, even if I log in to karaf console and type log:clear?