Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

issue a sudo command inside a automated SSH session

Status
Not open for further replies.

keak

Programmer
Sep 12, 2005
247
CA
I have a script that automates a SSH session,
In that session, I am trying to sudo to another user (it works if I type it in the command line), but when I run the script, I am getting the following error:


seudo-terminal will not be allocated because stdin is not a terminal.
Warning: no access to tty (Bad file descriptor).
Thus no job control in this shell.

Code:
$HOST="myhost.com"
ssh $HOST <<END_SCRIPT
mv testfile.pl /tmp
sudo -H -u anotherUser bash
cd ~/apache2/cgi-bin
cp /tmp/testfile.pl ./
exit
END_SCRIPT

is there any way around this error?
sudoers have been set up so that I can su to "anotherUser" without a password, as well as the SSH key (so that I can SSH over to the host without a pw.
 
I realized it works if I use
Code:
sudo -H -u anotherUser 
cp /tmp/testfile.pl /home/anotherUser/apache2/cgi-bin
but I still get the same error message.

Also, is it possible to specify the home directory of "anohterUser" without hardcoding it?
so something like
cp /tmp/testfile.pl ~/apache2/cgi-bin ?
when I did this, it tried reading the directory from my user, and not "anotherUser"'s home directory.
 
Try escaping the ~, i.e. \~?

Not sure about the tty thing, I would stick some echos in there to diagnose exactly which command is generating that error message; possibly something in anotherUser's .(bash_)profile?

Annihilannic.
 
Since your RSA key is installed for the user you wish to sudo to, you could try something like this:

Code:
$HOST="myhost.com"
$USER="anotherUser"
ssh $HOST <<END_SCRIPT_PART1
mv testfile.pl /tmp
exit
END_SCRIPT_PART1
ssh -l $USER $HOST <<END_SCRIPT_PART1
# sudo -H -u anotherUser bash
cd ~/apache2/cgi-bin
cp /tmp/testfile.pl ./
exit
END_SCRIPT

I believe there is a command line arg that will prevent ssh from trying to allocate a psuedo tty, that might help too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top