AlexxIT / SSHCommand

Run any SSH command on remote server from Home Assistant service call
https://github.com/AlexxIT/Blog
MIT License
93 stars 8 forks source link

Unable to natively execute sudo privileged commands #29

Open sigmaenigma opened 2 months ago

sigmaenigma commented 2 months ago

Hello. I found an issue on line 49 where the command does not include an option to run a command with sudo privileges. I modified your init.py script in HomeAssistant and verified this works. I can do a formal merge request if you'd like:

# If sudo is in the command, pass the password to execute
if 'sudo' in command:
    _, stdout, stderr = client.exec_command(f'echo {password} | sudo -S {command}')
    password = '**redacted**' # Redacting password for the response
else:
    _, stdout, stderr = client.exec_command(command)

I was trying to perform a 'sudo shutdown -h now' or 'sudo apt update' and noticed this failed but I knew I was successfully logging in.

sigmaenigma commented 2 months ago

Another observation I've made... if you have an "&" in the password, it fails.

AlexxIT commented 2 months ago

User can write command with echo and pipe if it's necessary.