Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker-compose execute scripts not working #1411

Open
solderchicken opened this issue Jan 16, 2025 · 18 comments
Open

docker-compose execute scripts not working #1411

solderchicken opened this issue Jan 16, 2025 · 18 comments

Comments

@solderchicken
Copy link

Hi all

Had habridge working great on a Pi, then on Ubuntu but now trying to get to work in docker-compose.

I have shutdown/reboot and WOL scripts that execute when I right click and 'run as program' in Ubuntu.

Have got ha-bridge running and can execute these scripts (which are stored in /config/scripts) but nothing happens when I do.

Permissions issue? I'm not sure how to fix as there is nothing in the log.

Thanks in advance

@solderchicken
Copy link
Author

here is an example of the script.

ha-bridge says it executes, but nothing happens. If I run the script manually in Ubuntu it works. The code also runs in terminal as user.

#!/bin/bash
ssh [email protected] sudo shutdown now &> /dev/null
exit 0

@arnieSkyNet
Copy link

Don't forget habridge runs as root so try running your script via su- rather than sudo

I've created a GitHub called launchprog that allows you to put into the habridge on/dim/off launchprog ssh [email protected] sudo shutdown now

I'm just finishing it with a -d (debug) option that will leave a window open, so you can see any errors as it runs from root but not pass on the -d as A parameter to the next program, launchprog passes it onto

@arnieSkyNet
Copy link

I've released it

https://github.com/arnieSkyNet/habrunasuser

If I shouldn't of posted here, please delete or tell me to delete and I will

@BuxtonCalvin
Copy link

You should check the container to make sure it includes SSH. If not, you'll have to add ssh to the container. If you're using the "lscr.io/linuxserver/habridge:latest" image, you can add ssh by including the following volume in your compose file.

home/custom-services:/custom-cont-init.d:ro # ssh service

Then in your local custom-services folder, add the file "add-ssh.sh" set the permissions to execute and add the following inside the file:

#!/bin/bash

echo "**** installing ssh client ****"
apk add --no-cache openssh-client

That should do it. There is info on the net for lscr containers on how to add custom services to their containers.

here is an example of the script.

ha-bridge says it executes, but nothing happens. If I run the script manually in Ubuntu it works. The code also runs in terminal as user.

#!/bin/bash ssh [email protected] sudo shutdown now &> /dev/null exit 0

@arnieSkyNet
Copy link

Basically the SSH keys are for the user not root user, hence ssh works via sudo in the terminal but not as root via habridge as root hasn't got the user keys.

That's why I wrote launchprog so habridge root can use the users keys

@solderchicken
Copy link
Author

solderchicken commented Jan 21, 2025

Hi both

Thank you so much for your input, sorry couldn't respond more promptly.

Have tried the method from @BuxtonCalvin but still no dice :(

I now have a folder called 'custom-services' in /home/username/Compose/habridge

In there is the file 'add-ssh.sh' which is executable (chmod +x)

Contents of the file are;

#!/bin/bash

echo "**** installing ssh client ****"
apk add --no-cache openssh-client

In habridge, my 'off items' Type is Execute Program/Script

The 'Target Items' is shutdown.sh

In /home/username/Compose/habridge/scripts there is an executable file called shutdown.sh

If I run as program, it works, if I run fro terminal (./shutdown.sh) it works

In habridge, it says it executes but nothing happens still.

Script is as follows

#!/bin/bash
ssh [email protected] sudo shutdown now &> /dev/null
exit 0

I tried various options swapping sudo for su- but stil didn't get anywhere.

Completely stumped,,, :(

@arnieSkyNet
Copy link

arnieSkyNet commented Jan 21, 2025

It's the ssh keys

habridge is running as root
root doesn't use your users ssh keys (and you probably shouldn't try to make it either) but doing sudo Will miss guide you, as it works as that user because that user has access to the ssh keys

You can test it failing by doing su -
then run your program and see what goes wrong (in effect, you are being HA bridge)

Note: root on the local machine habridge is running on

Took me ages in fact years to work out. This was the issue.

So then I wrote that software to make it easier for me and so it would work

The other easier way is su -
Then create root ssh keys for the remote machine, including doing the fingerprint.
Then run your command to see if it works as root (while still in "su -" session

If it works fine, then you can then test it with HA bridge

Like I said, I've created a program called launch product that does all this for you if you don't want to create SSH keys for root, which is a real pain because every time you add a new remote machine, do your user, you have to do the same again for root.

@BuxtonCalvin
Copy link

BuxtonCalvin commented Jan 21, 2025

You also need to add a volume for the ssh keys:

  • /root/.ssh:/root/.ssh

Since you know the script works, try running the script from inside the container command line. An easy way to get to it is to use the portainer console feature for the given container. You should be able to change directories from inside the container to your volume where you are holding your action scrips.

@arnieSkyNet
Copy link

That will work but worth thinking about Root SSH keys, especially in Docker environments, come with significant risks like privilege escalation, weak key management, and lack of isolation. Minimising root access, enforcing restrictions, and securing key storage are critical steps to mitigate risks.

@solderchicken
Copy link
Author

Thank you both again.

habridge is running as root
root doesn't use your users ssh keys (and you probably shouldn't try to make it either) but doing sudo Will miss guide you, as it works as that user because that user has access to the ssh keys

my ssh keys are exchanged as root user, not my logged in user. if habridge runs as root then it should work?

Also, my 'On' items scripts is just a simple wake on lan, does not use ssh, and this doesn't work either.

#!/bin/bash
wakeonlan eth0 b8:xx:xx:xx:xx:xx
wakeonlan wlan0 b8:xx:xx:xx:xx:xx
exit

If I run that script as program in linux it works fine, if I run it in habridge it says it executes but nothing happens.

I checked your link to the app but it was a 404?

@BuxtonCalvin
Copy link

You have to think in terms of a container is more or less completely isolated from the host machine. Everything that needs to be done by the container needs to be accessible to the container. So to get to anything outside the container, including scripts, you have to ssh into the host.

Here is a basic ssh command executed from inside a container:

ssh -i /root/.ssh/yourshhkey.pkt [email protected] sudo /home/scripts/wakeonlan.sh

Assuming that the execute permissions are set on wakeonlan.sh and the ssh key "yoursshkey" has rw rights for root, this should do the trick.

@solderchicken
Copy link
Author

Thank you again, I think I have to accept this is just above my understanding.

I just installed ha-bridge again manually and java, and everything works as expected.

It would be neat to use docker for the three apps I want this machine to run (PLEX, qbittorrent, habridge) but I just don't have the skills with docker and Linux.

Thank you both again for your contributions, will leave the post open in case it's helpful for anyone else to contine the thread.

Cheers!

@arnieSkyNet
Copy link

Just try
su -

Then run the command and see what error comes back. I suspect it's a key issue

@solderchicken
Copy link
Author

solderchicken commented Jan 24, 2025 via email

@arnieSkyNet
Copy link

Machine running habridge:
su -
(Enter password)
ssh [email protected]

If that logs into 192.168.1.152 without prompting for a password then you can exclude the ssh keys issue

So now while still logged into remote 192.168.1.152 do:

sudo shutdown now

If that works then try from habridge root user

ssh -tt [email protected] sudo shutdown now

@solderchicken
Copy link
Author

All three of those work!

@arnieSkyNet
Copy link

Ok so now we (and I should've done this before, but I'm getting old) we set up a tail on the remote machine to watch users login

So if on habridge and in terminal you've ssh into remote as root do
tail -f /var/log/auth.log
If not in as root
sudo tail -f /var/log/auth.log

auth.log is the Log file that monitors people logging in

Just check, it's inside /var/log/

Once that tail is running, you could always open another terminal I'm just a size 8 in and watch the screen, That way, you won't know if it is indeed login users accessing that machine

Next try via habridge web interface your command to see it is being executed, and whether the remote machine has noted a login

@arnieSkyNet
Copy link

Just a thought, try using fully qualified path for sudo and shutdown on the remote machine in the habridge web interface

/usr/bin/ssh -tt [email protected] /usr/bin/sudo /usr/bin/shutdown now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants