-1

So far I have this:

sshpass -p "password" ssh -q username@192.168.167.654 " [ "$(whoami)" != "root" ] && exec sudo -- "$0" "$@" ; whoami ; [run some commands as root]"

I keeps giving me username as answer from whoami. I want to be root as soon as I am connected to the server (but I can only connect to it with username). How can I be root throughout the connection to the server?

Clarification:

I want to access a remote server. It is mandatory that I connect as "username" and then switch to root to run and copy files that only root is able to do. So while I am connected to that server via ssh, I want to be root until my commands are over in the remote server. My problem is that I am not able to do so because I don't have the knowledge, hence I am posting it here.

Restrictions:

-can't use rsync.

-have to connect to the server as "username" and then switch to root

Redson
  • 2,098
  • 4
  • 25
  • 50
  • This is not the correct site/community for your question. Try it over at http://serverfault.com – nl-x Jun 04 '14 at 16:38
  • thanks, will post it thre – Redson Jun 04 '14 at 16:39
  • I'd argue that this is about programming, specifically how to programmatically elevate privileges through ssh. – that other guy Jun 04 '14 at 16:44
  • I don't know if it MUST be in bash. But you can take a look at [fabric](http://docs.fabfile.org/en/1.8/) this is what i use to execute stuff as user or root on a remote server (This is written in python) – drgn Jun 04 '14 at 16:54
  • Can you execute the "su -" command once you are connected (requires root password) - that will make you root for as long as the terminal is open – Matt Jun 05 '14 at 05:53

2 Answers2

0
sshpass -p "password" ssh -q username@192.168.167.654 exec sudo -s << "END"
  whoami
  commands to run
END
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • I get an error saying that 2 incorrect password attempts have been made but I haven't/couldn't entered any password – Redson Jun 04 '14 at 16:44
  • If you `sshpass -p "password" ssh -q username@192.168.167.654`, connect, and then enter `sudo -s`, does it ask for a password? – that other guy Jun 04 '14 at 16:45
0

You can try something like this (untested) But I've used the same concept to accomplish similar a similar task.

scp FileWithCommands.sh UserName@Hostname:/tmp
ssh Username@HostName "su -s -c /tmp/FileWithCommands.sh"
Hickory420
  • 131
  • 10