Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am able to connect to remote machine using public/private keys (without password) as user wakatana. On remote machine I am able to issue: sudo /usr/bin/rootsh -i -u root then I type root's password and I get root's permissions. Now I would like to "semi-automate" this process. By semi-automation I mean that I will still need pass root's password but also I will be able to pass another commands which will be executed under root permissions e.g. cat /etc/shadow

I have tried following command: ssh -t targetmachine "sudo /usr/bin/rootsh -i -u root; cat /etc/shadow" but this gives me the interactive root's prompt (after I type the password), and I was able to use the remote machine as if I was logged as root. Only after pressing ctrl+d it seems that it tries to open /etc/shadow because I get the message:

cat: /etc/shadow: Permission denied
Connection to targetmachine closed.

What I need is to somehow pass the cat /etc/shadow command to rootsh. But the problem is that I am not able to soduo any other command than /usr/bin/rootsh -i -u root

For example when I tried sudo /usr/bin/rootsh -i -u root -- cat /etc/shadow I get the:

Sorry, user wakatana is not allowed to execute '/usr/bin/rootsh -i -u root -- cat /etc/shadow' as root on targetmachine.

To sum it up, what I need is

  1. access the target machine:
  2. switch user to root on remote server (type password)
  3. execute command under this switched user
  4. close connection to remote server

Please note that I am not allowed to:

  1. edit sudoers file
  2. adding public/private keys to root
  3. allowing direct root login

Thank you

EDIT:

also I have tried suggested:

echo date | sudo /usr/bin/rootsh -i -u root

but it returns:

no tty/pty on stdin: Invalid argument
share|improve this question

Most shells will read from standard input even if it isn't a keyboard. See if this works on targetmachine:

$ echo date | sudo /usr/bin/rootsh -i -u root
[sudo] password for you: 
Mon Oct 14 13:29:09 EDT 2013

If so, you can see if using stdin works over ssh:

$ echo 'date ; whoami ; cat /etc/shadow' | ssh -t targetmachine 'sudo /usr/bin/rootsh -i -u root'

Or maybe send the commands directly into the remote sudo:

$ ssh -t targetmachine "echo 'date ; whoami ; cat /etc/shadow' | sudo /usr/bin/rootsh -i -u root"

Be careful to get the single/double quotes nested correctly. (My tests show that sudo doesn't work in the above examples because it can't read my local tty to get a password.)

share|improve this answer
    
Unfortunately this did not work for me – Wakan Tanka Oct 15 '13 at 15:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .