sudo inside a here document not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sudo inside a here document not working
# 8  
Old 11-12-2010
Can you explain that a little more? I am sorry, just started shell scripting!! How to redirect stdin to provide input to sudo?
# 9  
Old 11-12-2010
Quote:
Originally Posted by mnanavati
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!
did you try ssh -tt option in your script?
# 10  
Old 11-12-2010
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:

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


Last edited by Chubler_XL; 11-15-2010 at 12:23 AM..
# 11  
Old 11-18-2010
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.
# 12  
Old 11-21-2010
frank_rizzo,

mnanavati can't change the sudoers file:

Quote:
Originally Posted by mnanavati
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
# 13  
Old 11-22-2010
Quote:
Originally Posted by mnanavati
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 :
Code:
 
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!!
I think there are maybe some errors in your script
if you think everything is ok then you can try this

Code:
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
# 14  
Old 11-22-2010
Quote:
Originally Posted by Chubler_XL
frank_rizzo,

mnanavati can't change the sudoers file:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash - here document on RHEL 6.8 and 6.9 servers aren't working

H Forum, I was thankful in getting help from this post that allowed me to connect to multiple severs at once using here documents to gather data into variables. But I've discovered that the same bash command that works on my RHEL 7 servers do not work on RHEL 6? What's strange about my... (4 Replies)
Discussion started by: greavette
4 Replies

2. Solaris

Sudo Not working on Solaris 10

Hi All, im a bit new to Solaris 10 iv been working on Redhat and cant seem to get the sudo working on Solaris 10:D Iv installed the below packages via pkgadd command: gcc-4.7.2-sol10-x86-local libiconv-1.14-sol10-x86-local libintl-3.4.0-sol10-x86-local sudo-1.8.13-sol10-x86-local ... (1 Reply)
Discussion started by: SolarisRSA
1 Replies

3. Red Hat

sudo is not working properly

This is the first time for using sudo for me. # visudo ## Allows people in group admin to run all commands %admin ALL=(ALL) ALL # groupadd admin # useradd temp # usermod -a -G admin temp # id temp uid=506(temp) gid=506(temp) groups=506(temp),507(admin) # #sudo... (5 Replies)
Discussion started by: getrue
5 Replies

4. Shell Programming and Scripting

Here document inside a here document?

Can we use a here document inside a here document? Something like this ssh user@remotehost << REMOTE sudo vserver vsernamename enter << VSERVER perform actions on vserver. VSERVER REMOTE (6 Replies)
Discussion started by: mnanavati
6 Replies

5. UNIX for Dummies Questions & Answers

Problem with Sudo inside a here document

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 ... (0 Replies)
Discussion started by: mnanavati
0 Replies

6. Shell Programming and Scripting

sudo + ulimit not working ?

When I use sudo with ulimit there is an error but if I use ulimit without sudo there is no error. In bash: user1@debian:~$ sudo -u user2 -H ulimit -S -c unlimited sudo: ulimit: command not found user1@debian:~$ user1@debian:/home/user1$ ulimit -S -c unlimited user1@debian:/home/user1$... (3 Replies)
Discussion started by: cyler
3 Replies

7. Shell Programming and Scripting

sudo command is not working inside a script when placed in cron

Hi All, i have a cron entry like 0,3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57 * * * * /amex/sssmonitor/dss_chk.ksh and the script is like #!/bin/ksh file=`uname -n` > /sunmast/projects/oasis/COREDEV/Dss$file.log > /tmp/output_sss today=`date` varb=`ps -ef | grep... (5 Replies)
Discussion started by: usha rao
5 Replies

8. UNIX for Advanced & Expert Users

sudo chmod not working

now running mac os x 10.53 and sudo chmod comes back with : operation not permitted. sample file: -rwxr-xr-x+ 1 1000 com.apple.monitor_all_services 276592 Jun 8 2007 342345.tif i am running as root. (1 Reply)
Discussion started by: Movomito
1 Replies

9. Shell Programming and Scripting

here document not working

I tried doing ftp myhost <<HERE username password quit HERE but it doesnt work. Why? When I do ftp host, I always get prompted for username, and once I type that in I get prompted for password. But when I try doing it from here document it freezes. (2 Replies)
Discussion started by: JamesByars
2 Replies

10. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies
Login or Register to Ask a Question