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.

  • 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. – Kai Burghardt Nov 06 '22 at 22:48

1 Answers1

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

YonzLeon
  • 311
  • 1
  • 6
  • 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 – ossama assaghir Aug 19 '22 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 -S -v && sudo whoami);` – YonzLeon Aug 20 '22 at 09:09
  • Sadly posh-ssh doesn't support pseudo TTY on official repo: https://github.com/darkoperator/Posh-SSH/issues/68 (2015) You should see this too: https://github.com/darkoperator/Posh-SSH/issues/297 – YonzLeon Aug 20 '22 at 09:14
  • Hi @ossamaassaghir please vote 'up' if the information helping you – YonzLeon Aug 22 '22 at 15:06