Right script for executeCommandLine

Dear, my head hurt tooday, this must be something simple

I have little script that works for 2 yeasr that reboots openhab panels. it is just fine.

tooday i needed new script to reboot gate so I copied old one it and made appropriate button to execute it with a new rule. fine, old script copy works tested

-rwxrwxr-x 1 sherlock sherlock 620 Mar 17 15:36 rebootgate.sh
-rwxrwxrwx 1 sherlock sherlock 620 Apr  5  2022 rebootpanels.sh

I edited the new script. put command I need in it. and it does not execute very same command
like i I see the echo but the very same ssh will not execute.
What could that be?

[15:38:15] sherlock@openhab2:~$ more rebootpanels.sh
sshpass -p xxxxxxxx ssh admin@172.16.10.6 '(echo "enable" ; echo "configure" ; echo "interface 0/38" ; echo "poe opmode shutdown" ; echo "poe opmode auto" ; ec
ho "interface 0/40" ; echo "poe opmode shutdown" ; echo "poe opmode auto" ; echo "interface 0/41" ; echo "poe opmode shutdown" ; echo "poe opmode auto"; echo "inte
rface 0/42" ; echo "poe opmode shutdown" ; echo "poe opmode auto"; echo "interface 0/43" ; echo "poe opmode shutdown" ; echo "poe opmode auto"; echo "interface 0/4
4" ; echo "poe opmode shutdown" ; echo "poe opmode auto"; echo "exit" ; echo "exit"; echo "exit" ) | telnet localhost 23 ; exit;'
15:50:22] sherlock@openhab2:~$ more rebootgate.sh
echo "bla blah"
sshpass -p xxxxxxx ssh pi@172.16.10.196 'sudo systemctl restart gate.service ; exit;'

if I run it manualy it is just fine of course

What could be wrong
TIA

I think when you ran the script the first time you were prompted to accept the remote host’s fingerprint of the host’s key.
Running the script by with openhab user privileges the first time should prompt for it as well.
To get around it either run the command as user openhab to accept it and let it store in .ssh directory in user openhab home directory or use a slightly modified version:

echo "bla blah"
sshpass -p xxxxxxx ssh -o "StrictHostKeyChecking=no" pi@172.16.10.196 'sudo systemctl restart gate.service ; exit;'

Other problem could be that the sudo command returns something but as it is being run at the other end of ssh endpoint it is being executed with same privileges that you already tested so that should be fine.

In case the above does not work try to create debug output by redirecting stderr and stdout of the ssh command to a file.

This could be a reason, it is certainly a linux permisions thing.
however sudo -u openhab ./rebootgate.sh does not prompt me for the keys and it should.

Maciej

In first line of the script I would put in something like

#!/bin/sh

added #!/bin/sh that is not helping.
I also tried to add full path for sshpass and ssh. Not solving it.
Is there a way to interactively login as the openhab user and check the output of the script?

do

sudo -u openhab bash

then you should have a shell with privileges of user openhab.

1 Like

Thank you,
Logging interactively I could see the prompt for saving ssh keys, that were the issue here.
After one time ssh login now all executes fine.
The other script that was working dine was very old one and I must have done that previously and forgot.

That ( fingerprint hostkey ) is what my first reply was about… good to see you have it running now.