sudo inside a here document not working

Have a sudo statement inside of a here document. It prompts me for a password, but doesnt wait for me to enter my password. Is there a way I can use the command without sudo or anyway that I can enter the password correctly?

Eg :

while read  remotehost
do
  ssh -t $2@$remotehost  <<REMOTE

   ls /path/to/file/ | while read line
     do
              sudo  vserver \$line enter
              echo "\$line Hello world"
      done

REMOTE
done < "$remotehostlist"


This asks for password for the following line sudo vserver $line enter. But it doesnt wait for me to enter password. Also if I type something it appears in cleartext.

Please help!!

Is the password request coming from sudo or ssh. It seems like belt and suspenders (overkill). Just ssh from your normal ID to the host and id you want, if possible, passwordless using keys.

Unfortunately I cannot ssh with root credentials , and I cannot change the sudoers file on servers.
So i need to sudo only after I ssh

Well, if you ssh, the ssh is a terminal wrapper, I think, so you should be able to inject the sudo password via stdin.

BTW, the performance is better if "what you run under sudo takes in "a stream of file names", not one per.

what is your goal? only start vservers on remote and echo message?
and are you sure sudo permission on remote..?

what happened if you test sudo command on your prompt..
did it asked password for sudo ?

and if you know on the remote that is enabled the requiretty on sudo
maybe you try multiple t option

ssh -tt ...

Well the purpose is not to echo after sudoing into the vserver. The code inside there is proprietary and so I did not want to post it here

Secondly,
If I sudo on command prompt it works fine. Prompts me for a password and I enter the password an can enter the vserver correctly!

Does sudo actuall insist on a terminal explicitly, not implicitly for password? If b, can the sudo options for password from stdin -S or other source -A help ?

Can you explain that a little more? I am sorry, just started shell scripting!! How to redirect stdin to provide input to sudo?

did you try ssh -tt option in your script?

I've use something like this in the past, echo is a shell internal so no one will be able to see the password in a ps listing:

#!/bin/bash
read -sp "Password: " PASS
echo
echo $PASS | ssh account@myhost 'sudo somescript'

use NOPASSWD for sudo rule for batch jobs. don't use a HERE document that way. put the logic in a remote script and execute that remotely. also - add -n to your ssh loop.

frank_rizzo,

mnanavati can't change the sudoers file:

I think there are maybe some errors in your script
if you think everything is ok then you can try this

while read  remotehost
do
  ssh -t $2@$remotehost  <<REMOTE
 
   ls /path/to/file/ | while read line
     do
              sudo -k
              sudo  vserver \$line enter
              echo "\$line Hello world"
      done
 
REMOTE
done < "$remotehostlist"

regards
ygemici

that is unfortunate because doing it any other way is a hack IMO. I suggest do it right the first time. work smarter not harder.