1

I have a shell script that runs on a freebsd OS. the script has some commands that needs the sudo privileges

Im excuting the script remotley and it's skipping the sudo commands

i tried the following :

echo password | sudo -S Command

but it's not doing anything (same results)

note : i can't modify the sudoers file because it's an automated process that creates a virtual machine and excute the script once it's created

I'm usign posh-ssh to excute the script remotely :

#Excute Script 
$ExShellScript = 'echo "password" | sudo -S  sh script.sh 
Write-Output $ExShellScript
$Query2 = $(Invoke-SshCommand -SSHSession $SessionID  -Command $ExShellScript).Output
$Query2 = $Query2.split("`n")
Write-Output $Query2
Remove-SSHSession -Name $SessionID | Out-Null

i would apreciate it if someone could guide me to the right direction because im stuck here.

1
  • How about running the entire script as root, but lose privileges using sudo(8) when root isn’t required? This won’t require setting up a proper sudoers(5) file. Nov 6, 2022 at 22:48

1 Answer 1

3

can you try this :

echo "<your_password>" | sudo -S -v

after that sudo should will run without asking for a password

enter image description here

Im suggest you to run the command directly before implement on the remote script to make sure its running well

4
  • Hi @YonzLeon i tried it but i got the same results, for more context i'm using posh-ssh to excute the script remotely i added a screen shot of the code im using Aug 19, 2022 at 22:40
  • I see, when im try runing the script remotely its prompt error : sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper. It seems running command remotely for sudo priveleges need TTY for password prompt. solved with flag -t when running remote script, ex: ssh user@host -t "bash script.sh" Here my script : test=$(echo "<sudo-password>" | sudo -S -v && sudo whoami);
    – YonzLeon
    Aug 20, 2022 at 9:09
  • Sadly posh-ssh doesn't support pseudo TTY on official repo: github.com/darkoperator/Posh-SSH/issues/68 (2015) You should see this too: github.com/darkoperator/Posh-SSH/issues/297
    – YonzLeon
    Aug 20, 2022 at 9:14
  • Hi @ossamaassaghir please vote 'up' if the information helping you
    – YonzLeon
    Aug 22, 2022 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.