Execute SSH command

Hi everyone,

I try to disable a specific group of my Pihole dns. As the api of pihole is not useful I need to do this with the following SSH command.

I found it in a pihole thread.

So I created a rule for a specific item with the following value
ssh root@pihole sudo sqlite3 /etc/pihole/gravity.db “update ‘group’ set ‘enabled’=0 where name=‘Lena’;” ; /usr/local/bin/pihole restartdns reload-lists >/dev/null

But this seems not to work.
When I execute the following line by line in a ssh session from openhab it is working.

ssh root@pihole
sudo sqlite3 /etc/pihole/gravity.db
update ‘group’ set ‘enabled’=0 where name=‘Lena’;
.quit
/usr/local/bin/pihole restartdns reload-lists >/dev/null

I added the .quit and modified the " in the update statement but it is working as expected.
Now I need to integrate it in openhab but have some troubles. Maybe someone has an hint for me.

Regards
Eric

In my opinion, in these cases it is almost always easiest to put the command series in a shell script and run that script from the rule with executeCommandLine.

1 Like

As @JustinG already mentioned - putting the command in a script and execute it via executeCommandLine within a rule is the easiest way.

But if you still want to do it via the exec binding - you have to whitelist your command and make it executable via the openhab user as described in the docs

And to save you some headache - add an extra line after each command and restart openhab after updating your exec.whitelist. I dont know the reason and it shouldn´t be needed, but that works for me.

When executing a command from the Exec binding or executeCommandLine you don’t have a shell. Things like ;, | and > are all implemented by the shell so they simply do not exist. The easiest way to handle this is to follow the suggestions here and put the commands into a script. That will give you commands a shell to run into. That’s what the first line of the script does, it tells it what program to execute the commands in. #!/bin/bash means run the commands in the shell bash.